iOS10推送适配完整说明

一年一度的iOS大版本更新又开始了,对于不明真相吃瓜群众来说真是太好啦!对于我们程序员却意味着disaster...这次的推送架构完全推翻以往,所以得从新适配,话不多说,开始吧。 1.在targets的Capabiliies内Push Notifications选项开关打开

然后Background Modes打开如下几个选项

友情提示上图几个选项,如果你应用内没有需要在后台音频播放或者位置更新,第一和第二项还是别勾上了,免得被App Store审核bb...我的刚提交两天就给我干下来返工了,555

General内导入UserNotifications.framework

2.进入Appdelegate.m文件

2.1) #import <UserNotifications/UserNotifications.h>

遵循UNUserNotificationCenterDelegate协议

@interface AppDelegate()<UNUserNotificationCenterDelegate>

2.2)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法内调用registRemoteNotifications方法

//20160930 注册通知APNS

[self registRemoteNotifications];

该方法具体如下

1.- (void)registRemoteNotifications {
    
    if ([[[UIDevice currentDevice] systemVersion]floatValue]>=10.0) {
        //申请用户同意
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"succeeded!");
            }
            if (granted) {
                
                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    
                    NSLog(@"remoteNotificationSetting: %@", settings);
                    
                }];
            }
        }];
        
    }
    float ios_version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    if (ios_version >= 8.0){//iOS8-iOS10
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
    }
    
}
复制代码

2.3) 再实现如下两个代理方法 #pragma mark --ios10推送回调 //前台回调

1)- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification
*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler

{

    [self application:[UIApplication sharedApplication] didReceiveRemoteNotification:notification.request.content.userInfo];

}

//后台回调

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

{

    [self application:[UIApplication sharedApplication] didReceiveRemoteNotification:response.notification.request.content.userInfo];

}
复制代码

完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值