iOS10推送适配完整说明

作者: Auditore | 来源:发表于2016-10-09 14:17 被阅读331次

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


Paste_Image.png

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

Paste_Image.png

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

General内导入UserNotifications.framework

Paste_Image.png

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];

}

完成

相关文章

  • iOS10推送适配完整说明

    一年一度的iOS大版本更新又开始了,对于不明真相吃瓜群众来说真是太好啦!对于我们程序员却意味着disaster.....

  • 记录对iOS10的适配工作ʕ •ᴥ•ʔ

    iOS10正式版开始推送,赶着ddl做的iOS10适配工作也算是完成了,这里记录下iOS10适配中做的工作。 仅是...

  • iOS10适配远程推送

    iOS10正式版发布之后,网上各种适配XCode8以及iOS10的文章满天飞。但对于iOS10适配远程推送的文章却...

  • iOS10 适配远程推送功能实现代码

    iOS10正式版发布之后,网上各种适配XCode8以及iOS10的文章满天飞。但对于iOS10适配远程推送的文章却...

  • iOS开发相关技术文章

    适配iOS10以及Xcode8 21个高质量的Swift开源iOS App iOS10推送必看UNNotifica...

  • iOS10推送通知整理总结

    这篇文章整理iOS10之后的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以后的推送通知) iOS10...

  • iOS推送通知学习与总结

    这篇文章整理iOS10之前的推送通知(文中的推送通知,如不做特殊说明,默认是iOS10以前的推送通知) iOS10...

  • iOS10自定义推送UI和推送内容(上)

    前言 iOS10这个系统已经出来一年多了,那时候系统刚发布的时候也做过iOS10推送的适配,但是那时候推送是集成第...

  • 适配iOS10推送

    直接上代码 swift版本 import the UserNotifications framework and ...

  • JPush 远程通知集成

    注:此文只现在已经不能适配iOS10了,iOS10推送采用了新的方法,做iOS9及以下的系统可读此篇文章; iOS...

网友评论

    本文标题:iOS10推送适配完整说明

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