iOS 开发每天分享优质文章

iOS10: 个推接入

2017-06-07  本文已影响744人  L_zix

个推的工作流程如下: 

推送流程

接入步骤

1. 证书创建

2. 推送平台创建APP

3. 代码搭建


证书创建

1. 要有开发证书( certificate + appID )

一般先创建AppID: 创建一个通配符: com.companyName.*

再创建Certificate: 使用上述AppID创建, CSR文件电脑创建, 下载下来双击.

开发证书

2. 创建推送证书( certificate + AppID )

还是先得创建一个AppID: 这里必须要创建"全名"ID, 不能使用通配符. 这时创建的时候要选择Push Notification功能

记得勾选

创建完AppID之后, 发现Push Notification是黄色图标的, 还欠配置. 完成下面一步之后就可以了.

还是在certificate里, 创建一个推送证书, 还是需要CSR文件, AppID则选择刚刚创建的全名ID. 

推送证书

创建完成之后, 双击安装证书, 并导出p12文件.(待会第二步消息平台有用), 设置完密码和名字之后, 把他放好待会用

导出p12文件

3. 创建DeviceID

UDID可以在iTunes获取, 然后点击下一步创建就好

4. 创建profile文件

certificate + AppID + DeviceID

certificate 选开发者证书(反正你选不了推送证书)

AppID 选全名ID

DeviceID全选都行

之后下载profile文件, 双击执行

配置Xcode:

要打开Push Notification功能

打开Background Mode功能, 并勾选Notification和BackgroundFetch

2. 推送平台创建APP

3. 代码搭建

1. 初始化SDK

[GeTuiSdkstartSdkWithAppId:GETUI_APPIDappKey:GETUI_APPKEYappSecret:GETUI_APPSECRETdelegate:self];

APPKey, APPID, APPSecret 是在第二步消息推送平台创建APP之后给的

2. 注册远程推送

自iOS10出来之后, 注册远程推送有3个方法, UNUserNotificationCenter , UIUserNotification, UIRemoteNotification

UNUserNotificationCenter: (iOS10之后)

UNUserNotificationCenter*center = [UNUserNotificationCentercurrentNotificationCenter];

center.delegate=self;

[centerrequestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSoundcompletionHandler:^(BOOLgranted,NSError*_Nullableerror) {

if(!error) {

NSLog(@"authorize succeed!");

}

}];

[[UIApplicationsharedApplication]registerForRemoteNotifications];

UIUserNotification:(iOS8之后)

UIUserNotificationTypetype = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeBadge;

UIUserNotificationSettings*setting = [UIUserNotificationSettingssettingsForTypes:typecategories:nil];

[[UIApplicationsharedApplication]registerForRemoteNotifications];

[[UIApplicationsharedApplication]registerUserNotificationSettings:setting];

UIRemoteNotification: 

UIRemoteNotificationTypetype = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;

[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:type];

注册成功之后, 会走APPDelegate的一个回调didRegisterForRemoteNotificationsWithDeviceToken

在这个回调里, 需要执行下面代码: 处理DeviceToken

NSString*deviStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];

deviStr = [deviStrstringByReplacingOccurrencesOfString:@" "withString:@""];

[GeTuiSdkregisterDeviceToken:deviStr];

因为开启了后台模式里的BackgroundFetch, 所以下面AppDelegate回调会执行

- (void)application:(UIApplication*)application performFetchWithCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

[GeTuiSdkresume];

completionHandler(UIBackgroundFetchResultNewData);

}

之后还要处理接收到通知的回调: 

接受通知的回调有两种, 要看app是通过那个API完成远程推送的注册, 是UNUserNotificationCenter还是UIUserNotification

若是前者, 则使用以下回调处理点击通知: 

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

NSLog(@"UserNotificationDidReceive: %@", response.notification.request.content.userInfo);

[GeTuiSdkhandleRemoteNotification:response.notification.request.content.userInfo];

completionHandler();

若是后者, 则使用以下回调处理点击通知:

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

//个推处理数据,收到推送

NSLog(@"AppDelegateDidReceive: %@", userInfo);

[GeTuiSdkhandleRemoteNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);

}

以上便完成个推的接入

上一篇下一篇

猜你喜欢

热点阅读