IOS10 远程推送适配详细介绍
IOS10远程推送适配
iOS10推送新增了UserNotificationsFramework,使用起来其实很简单。
建议看看极光推送的Demo,里面写的更详细。
只是在iOS10以上系统上点击通知栏,回调方法不再走原来的这两个方法
-(void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary*)userInfo{}
-(void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary*)userInfo
fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler{}
而是在前台的时候回调
-(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^) (UNNotificationPresentationOptions))completionHandler
从后台进入的时候回调
-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler
直接说怎么用吧:
1,导入头文件
#ifdefNSFoundationVersionNumber_iOS_9_x_Max #import<UserNotifications/UserNotifications.h> #endif
2,注册通知
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions方法中
if(iOS10){
UNUserNotificationCenter*center=[UNUserNotificationCentercurrentNotificationCenter];
center.delegate=self;
[centerrequestAuthorizationWithOptions:(UNAuthorizationOptionBadge
|UNAuthorizationOptionSound|UNAuthorizationOptionAlert)
completionHandler:^(BOOLgranted,NSError*_Nullableerror){
if(!error){
NSLog(@"succeeded!");
}
}];
}elseif(iOS8_9){//iOS8-iOS9
UIUserNotificationSettings*settings=[UIUserNotificationSettings
settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert
|UIUserNotificationTypeSound)categories:nil];
[applicationregisterUserNotificationSettings:settings];
[applicationregisterForRemoteNotifications];
}else{//iOS8以下
[applicationregisterForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];
}
3,回调方法中,获取通知数据
-(void)userNotificationCenter:(UNUserNotificationCenter*)center
didReceiveNotificationResponse:(UNNotificationResponse*)response
withCompletionHandler:(void(^)())completionHandler{
NSDictionary*userInfo=response.notification.request.content.userInfo;
//消息处理
if([request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]){
//判断为远程推送
}else{
//判断为本地通知
}
}
4,对于本地通知没有什么变化依然会回调
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下