1.推送权限
对于远程推送而言,我们可以直接调用[application registerForRemoteNotifications];来获取deviceToken,传给后台。并不需要用户在“通知权限弹框中”允许。
“通知权限弹框”真正询问的是 是否允许 推送有弹框、声音、角标。
2.注册推送
依赖头文件<UserNotifications/UserNotifications.h>
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// UNUserNotificationCenterDelegate
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 点击允许
NSLog(@"注册成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
} else {
// 点击不允许
NSLog(@"注册失败");
}
}];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){
//iOS8 - iOS10
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
}else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
//iOS8系统以下
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
// 注册 获取device Token
[[UIApplication sharedApplication] registerForRemoteNotifications];
3.远程推送回调代理方法
*iOS10以前
1. UIApplication中有两个关于推送通知的代理回调:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, ");
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);
2. 二者的区别:调用时机不同
didReceiveRemoteNotification 只会在APP 处于运行中(前台、后台)时,会调用。
didReceiveRemoteNotification fetchCompletionHandler 会在APP处于运行中 或者被杀死状态都会被调用。
3. 注意
当你两个方法都实现了 ,系统只会调用didReceiveRemoteNotification fetchCompletionHandler
*iOS 10以后
1.UIApplication中多了UNUserNotificationCenterDelegate的三个代理回调方法
(1)app在前台收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14);
(2)点击通知栏
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14) __TVOS_PROHIBITED;
(3)打开通知设置
- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification __IOS_AVAILABLE(12.0) __OSX_AVAILABLE(10.14) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
4. 发送推送格式
(1)普通推送
{"aps":{"alert":"测试推送","badge":3,"sound": "default"},"json":"自定义结构"}
(2)静默推送
{"aps":{"content-available": 1},"json":"自定义结构"}