iOS本地通知

2017-07-21  本文已影响0人  ROTK_lvzhenhua

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信息

通知类型

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

添加和移除通知

添加通知

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

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

移除通知

- (void)removePendingNotificationRequestsWithIdentifiers:(NSArray<NSString *> *)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去识别唯一的设备标识

成功或者失败

写在最后

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

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

猜你喜欢

热点阅读