IOS10中关于通知的适配问题

在更新了iOS10之后发现在通知部分出现了一些问题,原先的处理是:锁屏状态下接收到评论的推送通知之后滑动打开应用可以进入该评论对应的帖子内容。但在iOS10之下再做这个操作就只能进入应用,而无法定位到特定的某个页面。经过调查后发现是iOS10在更新推送通知后的适配问题,iOS10开始增加了UNUserNotificationCenter,并且推送通知的处理要在代理方法userNotificationCenter: didReceiveNotificationResponse(后台)和userNotificationCenter: willPresentNotification(前台)中进行处理。

下面总结一下处理方法:

1,导入头文件

#import <UserNotifications/UserNotifications.h>

2,定义一下判断系统版本的宏

#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)

3,遵守UNUserNotificationCenterDelegate代理

appDelegate.m:

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@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 *)notification
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值