iOS-極光推送開發小結

躍然發表於2015-10-27

一、我的開發

- (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> 呼叫完成的處理程式碼時,應用的介面縮圖會自動更新

注意:接收到遠端通知到執行完網路請求之間的時間不能超過30if (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);
}

三、證書相關
開發者賬號介面.png

相關文章