//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
遠(yuǎn)程推送應(yīng)用配置過(guò)程
1.創(chuàng)建支持遠(yuǎn)程推送功能的App ID
2.申請(qǐng)開(kāi)發(fā)者證書(shū),并選中剛剛創(chuàng)建的App ID
3.下載CER文件,并導(dǎo)入鑰匙串管理
4.申請(qǐng)發(fā)布證書(shū),并選中剛剛創(chuàng)建的App ID
5.下載CER文件,并導(dǎo)入鑰匙串管理
6.檢查App ID梧疲,確認(rèn)證書(shū)已經(jīng)指定
遠(yuǎn)程推送應(yīng)用程序開(kāi)發(fā)過(guò)程
1.新建應(yīng)用程序
2.指定AppID,在developer.apple.com上設(shè)置的AppID
#ifdef __IPHONE_8_0
//注冊(cè)接收通知的類型
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
//注冊(cè)允許接收遠(yuǎn)程推送通知
[application registerForRemoteNotifications];
#else
//如果是iOS7.0,使用以下方法注冊(cè)
[application registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound];
#endif
//當(dāng)?shù)玫教O(píng)果的APNs服務(wù)器返回的DeviceToken就會(huì)被調(diào)用
// 7040f7d5 5a974598 c5cf31b5 3e340b39 68affd25 122f0ce1 3f315226 396c2e5b
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken是:%@", deviceToken);
}
//接收到遠(yuǎn)程通知帮毁,觸發(fā)方法和本地通知一致
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"%@", userInfo);
}
***使用后臺(tái)的遠(yuǎn)程消息推送
1>在Capabilities中打開(kāi)遠(yuǎn)程推送通知
2>實(shí)現(xiàn)代理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
遠(yuǎn)程消息數(shù)據(jù)格式:
{"aps": {"content-available":1},"content-id":42}
執(zhí)行completionHandler有兩個(gè)目的
1>系統(tǒng)會(huì)估量App消耗的電量,并根據(jù)傳遞的UIBackgroundFetchResult參數(shù)記錄新數(shù)據(jù)是否可用
2>調(diào)用完成的處理代碼時(shí)豺撑,應(yīng)用的界面縮略圖會(huì)自動(dòng)更新
注意:接收到遠(yuǎn)程通知到執(zhí)行完網(wǎng)絡(luò)請(qǐng)求之間的時(shí)間不能超過(guò)30秒
if(userInfo) {
intcontentId = [userInfo[@"content-id"] intValue];
ViewController *vc = (ViewController *)application.keyWindow.rootViewController;
[vc loadDataWithContentID:contentId completion:^(NSArray *dataList) {
vc.dataList = dataList;
NSLog(@"刷新數(shù)據(jù)結(jié)束");
completionHandler(UIBackgroundFetchResultNewData);
}];
}else{
completionHandler(UIBackgroundFetchResultNoData);
}