iOS-極光推送開發小結
一、我的開發
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
......
......
......
// 程式在死亡狀態(殺掉程式),再次啟動,收到推送通知,跳轉至對應頁面
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
self.notificationUserInfo = userInfo;
// 這裡延遲1秒 否則不執行跳轉
// 媽蛋,為了這個,廢了我N個小時。測試時使用了通知 本地通知等多種方式均未成功。
// 程式啟動的過程中,直接執行push maybe 出錯,原理現在還不明瞭
// 執行個事件,就開啟個執行緒?像點選按鈕就觸發一個執行緒?
[self performSelector:@selector(skipToMessageCenter) withObject:nil afterDelay:1];
}
return YES;
}
#pragma mark - 遠端推送接受方法(極光推送)
// 程式在未死亡狀態,通知到來,會執行此代理,這裡做跳轉就好
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
if(![[User sharedInstance] isRefuseJPush]){
}
} else if (application.applicationState == UIApplicationStateInactive) {
if(![[User sharedInstance] isRefuseJPush]){
}
MessageModel * messageModel = [[MessageModel alloc] initWithNoticeDic:userInfo];
// BOOL isSaveSuccess = [[CustomMessageRepository alloc] insertCustomMessage:messageModel];
// if (isSaveSuccess) {
// NSLog(@"快取通知成功!");
// }else{
// NSLog(@"快取通知失敗!");
// }
MessageToSpecificViewController * messageToSpecificViewController = [[MessageToSpecificViewController alloc] init];
[messageToSpecificViewController messageFromViewController:self.rootViewController toSpecificViewControllerWithMessage:messageModel];
}
[self gainUserInfoDicWithDic:userInfo];
[APService handleRemoteNotification:userInfo];
}
-(void)skipToMessageCenter {
dispatch_async(dispatch_get_main_queue(), ^{
// 根據通知內容,跳轉至不同頁面
MessageModel * messageModel = [[MessageModel alloc] initWithNoticeDic:self.notificationUserInfo];
MessageToSpecificViewController * messageToSpecificViewController = [[MessageToSpecificViewController alloc] init];
[messageToSpecificViewController messageFromViewController:self.rootViewController toSpecificViewControllerWithMessage:messageModel];
});
}
注:因為推送缺陷,被黑成狗了。所以做什麼事情要盡心盡力做好。認真、負責…
二、遠端推送相關
遠端推送應用配置過程
1. 建立支援遠端推送功能的App ID
2. 申請開發者證書,並選中剛剛建立的App ID
3. 下載CER檔案,並匯入鑰匙串管理
4. 申請釋出證書,並選中剛剛建立的App ID
5. 下載CER檔案,並匯入鑰匙串管理
6. 檢查App ID,確認證書已經指定
遠端推送應用程式開發過程
1. 新建應用程式
2. 指定AppID,在developer.apple.com上設定的AppID
#ifdef __IPHONE_8_0
// 註冊接收通知的型別
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
// 註冊允許接收遠端推送通知
[application registerForRemoteNotifications];
#else
// 如果是iOS7.0,使用以下方法註冊
[application registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound];
#endif
// 當得到蘋果的APNs伺服器返回的DeviceToken就會被呼叫
// 7040f7d5 5a974598 c5cf31b5 3e340b39 68affd25 122f0ce1 3f315226 396c2e5b
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken是:%@", deviceToken);
}
// 接收到遠端通知,觸發方法和本地通知一致
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"%@", userInfo);
}
*** 使用後臺的遠端訊息推送
1> 在Capabilities中開啟遠端推送通知
2> 實現代理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
遠端訊息資料格式:
{"aps" : {"content-available" : 1},"content-id" : 42}
執行completionHandler有兩個目的
1> 系統會估量App消耗的電量,並根據傳遞的UIBackgroundFetchResult 引數記錄新資料是否可用
2> 呼叫完成的處理程式碼時,應用的介面縮圖會自動更新
注意:接收到遠端通知到執行完網路請求之間的時間不能超過30秒
if (userInfo) {
int contentId = [userInfo[@"content-id"] intValue];
ViewController *vc = (ViewController *)application.keyWindow.rootViewController;
[vc loadDataWithContentID:contentId completion:^(NSArray *dataList) {
vc.dataList = dataList;
NSLog(@"重新整理資料結束");
completionHandler(UIBackgroundFetchResultNewData);
}];
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
三、證書相關
相關文章
- iOS —— 極光推送和極光IMiOS
- Flutter應用整合極光推送Flutter
- 在 Laravel 中使用 極光推送Laravel
- 極光筆記|極光推送在APICloud平臺的使用教程筆記APICloud
- Laravel 極光推送驅動,使用極光不再那麼麻煩!Laravel
- APP訊息推送 極光推送 示例程式碼APP
- .NET對接極光訊息推送
- .NET快速對接極光訊息推送
- 極光筆記丨iOS 15推送新特性筆記iOS
- 極光推送申請iOS推送證書p12及配置流程iOS
- 極光推送申請iOS推送證書p12及配置教程iOS
- [外掛擴充套件]APP極光推送外掛!套件APP
- 快捷地整合極光推送(JPush)到 Laravel 專案中Laravel
- 極光推送demo在android studio中無法執行Android
- iOS-日常開發常用巨集定義iOS
- Flutter 開發小結Flutter
- django開發小結Django
- 暫存器,觸發器,三極體小結觸發器
- Flutter 開發小結 | TipsFlutter
- 小程式開發總結
- Vue元件開發小結Vue元件
- 【Flutter 專題】63 圖解 Flutter 整合極光 JPush 小結|8月更文挑戰Flutter圖解
- Taro小程式開發總結
- mpvue開發小程式總結Vue
- 移動端開發小結
- 小程式開發技巧總結
- 極光筆記 | 極光服務的信創改造實踐筆記
- 極光筆記 | 極光推出“運營增長”解決方案,開啟企業增長新引擎筆記
- 在Android裝置上使用極光推送id重複的原因分析和解決辦法Android
- 共赴璀璨約定!《光與夜之戀》極光測試今日開啟
- 極光筆記丨Spark SQL 在極光的建設實踐筆記SparkSQL
- C#爬蟲開發小結C#爬蟲
- React + TypeScript + Taro前端開發小結ReactTypeScript前端
- 微信小程式開發總結微信小程式
- 小程式開發實踐總結
- 9小時搞定微信小程式開發-高磊-極客時間微信小程式
- Flutter 跨平臺框架應用實戰-2019極光開發者大會Flutter框架
- cad.net開發小結——層次結構
- 極光筆記 | 極光PUSH服務助力企業提升搶單速度筆記