美文网首页
iOS10关于通知的适配

iOS10关于通知的适配

作者: RunningMan_Fly | 来源:发表于2016-10-10 19:00 被阅读458次

在更新了iOS10之后发现在通知部分出现了一些问题,原先的处理是:锁屏状态下接收到评论的推送通知之后滑动打开应用可以进入该评论对应的帖子内容。但在iOS10之下再做这个操作就只能进入应用,而无法定位到特定的某个页面。经过调查后发现是iOS10在更新推送通知后的适配问题,iOS10开始增加了UNUserNotificationCenter,并且推送通知的处理要在代理方法userNotificationCenter: didReceiveNotificationResponse(后台)和userNotificationCenter: willPresentNotification(前台)中进行处理。下面总结一下处理方法:1,导入头文件#import112,定义一下判断系统版本的宏#define IS_IOS7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)#define IS_IOS8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#define IS_IOS10_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)1231233,遵守UNUserNotificationCenterDelegate代理appDelegate.m:@interface AppDelegate ()@end

并且在application: didFinishLaunchingWithOptions方法中注册一下推送通知。

if (IS_IOS10_OR_LATER) {  //IOS10 之后采用UNUserNotificationCenter注册通知

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

center.delegate = self;

[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (!error) {

[[UIApplication sharedApplication] registerForRemoteNotifications];

NSLog(@"succeeded!");

}

}];

}else{ //IOS10 之前注册通知

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert)

categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

[[UIApplication sharedApplication] registerForRemoteNotifications];

}

}

4,实现接收推送消息的回调方法,iOS10之前使用application: didReceiveRemoteNotification 来进行回调处理,而在iOS10里则要实现UNUserNotificationCenterDelegate的两个代理方法:

userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification和

userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse。

#pragma mark - 推送消息接收

//IOS10之前 推送消息接收

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

Log(@"userInfo: %@", userInfo.description);

if ( application.applicationState == UIApplicationStateActive) {// 程序在运行过程中受到推送通知

// TODO

} else { //在background状态受到推送通知

// TODO

}

completionHandler(UIBackgroundFetchResultNewData);

}

//IOS10之后 推送消息接收

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{

NSDictionary *userInfo = response.notification.request.content.userInfo;

if ( [UIApplication sharedApplication].applicationState == UIApplicationStateActive) {// 程序在运行过程中受到推送通知

// TODO

} else { //在background状态受到推送通知

// TODO

}

completionHandler(UIBackgroundFetchResultNewData);

}

5,对于本地通知没有什么变化依然会回调

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notific

相关文章

  • iOS10关于通知的适配

    在更新了iOS10之后发现在通知部分出现了一些问题,原先的处理是:锁屏状态下接收到评论的推送通知之后滑动打开应用可...

  • iOS 10 的适配问题

    前言 随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,...

  • 简单解决iOS10适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS 10 的适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS10的适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS 10 的适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS 适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS 10 的适配问题

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有...

  • iOS10 新特性 学习笔记(一)-持续更新

    随着iOS10发布,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,...

  • iOS 10适配

    随着iOS10的发布,App的适配成为大家迫切需要解决的问题,下面总结了一些关于iOS10适配需要注意的问题。 1...

网友评论

      本文标题:iOS10关于通知的适配

      本文链接:https://www.haomeiwen.com/subject/dedvyttx.html