iOS本地通知

UserNotifications

Xcode需要打开后台推送的通知权限

首先这是一个系统级别的单利获得

[UNUserNotificationCenter currentNotificationCenter]

请求权限

得到系统的单利,去请求是否有通知授权

[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted){
            NSLog(@"通知权限请求成功");
        }else {
             NSLog(@"通知权限请求失败");
        }
    }];

get通知的一些配置信息

- (void)getNotificationSettingsWithCompletionHandler:(void(^)(UNNotificationSettings *settings))completionHandler
  //通知配置等信息

注册

内容

UNMutableNotificationContent->UNNotificationContent->NSObject

设置对应的content信息

  • title->标题

  • subtitle->子标题

  • body->内容

通知类型

  • 本地通知
    • UNTimeIntervalNotificationTrigger 时间触发器
    • UNLocationNotificationTrigger 区域触发器
    • UNCalendarNotificationTrigger 日历触发器
  • 远程通知

添加通知到对应的通知请求里面,每一个请求都有一个Identifier,移除的时候需要根据identifier去移除.

添加和移除通知

添加通知

- (void)addNotificationRequest:(UNNotificationRequest *)request withCompletionHandler:(nullable void(^)(NSError *__nullable error))completionHandler

添加的时候有一个对应的identifier

移除通知

- (void)removePendingNotificationRequestsWithIdentifiers:(NSArray *)identifiers

另外还有两个代理方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
   //将要请求通知的时候
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
   //收到点击请求的时候的通知
}

RemoteNotification(iOS8-10)

注册远程通知

[[UIApplication sharedApplication]registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]

把对应的deviceToken推送到对应的苹果服务器,根据这个deviceToken去识别唯一的设备标识

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

成功或者失败

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;​- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

  • (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler //处理对应的消息
    还有集成第三方的SDK,方便快速集成,但是可能自定义不是很丰富
    腾讯信鸽 阿里云 等等
    推送测试工具

写在最后

跳转app代码
这个是在ios8之后加入的,如果是ios8之前参考 app跳转
UIApplication还有一些对应的位置,icloud等的一些:

if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                     }

你可能感兴趣的:(iOS本地通知)