之前項目集成極光推送,遇到了一些問題,現(xiàn)在閑下來,總結(jié)一下分享給大家。
我在這里主要分享的是跳轉(zhuǎn)的實現(xiàn)和優(yōu)化AppDelegate.h里的代碼。其他的集成和什么的簡書上已經(jīng)有很多的大神發(fā)表過了,你們隨便找找就可以了。
直接入正題,平時我們接受到遠程推送的時候,點擊消息分兩種情況。
一. 程序沒殺死的狀態(tài)下,走的是以下極光的這個代理方法。
iOS 9 之后:
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler ;
iOS 9 之前:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
二. 程序已經(jīng)殺死的狀態(tài)下,走的是appDelegate的launchOptions方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
所以跳轉(zhuǎn)處理要處理要在這兩個地方做處理,因為我項目是做push跳轉(zhuǎn)的,在兩個地方做跳轉(zhuǎn)處理的時候,要做區(qū)分。
在APP被殺死的情況下,點擊遠程推送,因為主界面沒初始化完成,要做延時處理。這里用了一個笨方法。延時調(diào)用,具體時間要看你們APP的啟動速度。
[self performSelector:@selector(pushToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];
下面的是整個流程的代碼實現(xiàn)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//其他邏輯代碼省略.......
//注冊消息處理函數(shù)的處理方法
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types=JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption: launchOptions appKey:@"極光推送的Key"
channel: @"iPhone"
apsForProduction: 0
advertisingIdentifier: nil];
// 跳轉(zhuǎn)的邏輯代碼
// 如果是點擊消息推送啟動APP
if(receiveReuserInfo && [[NSUserDefaults standardUserDefaults] objectForKey:kXSUserID] ){
[self performSelector:@selector(pus hToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];// 1.0是延時跳 轉(zhuǎn),具體的參考你們APP的啟動速度
}
return YES;
}
#pragma mark - iOS7.0 later notification suport
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
[JPUSHService handleRemoteNotification:userInfo];
if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 && application.applicationState >=1 ) {
NSLog(@"ios 7.0 later后臺接收遠程推送");
[self pushToTargetVCWithdentifier:userInfo];
}else{
NSLog(@"ios 7.0 later前臺接收遠程推送");
}
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - JPUSHRegisterDelegate iOS10.0 later suport
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的請求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]] ) {
[JPUSHService handleRemoteNotification:userInfo];
if([UIApplication sharedApplication].applicationState >= 1){
[self pushToTargetVCWithdentifier:userInfo]; // 跳轉(zhuǎn)處理
}
}else{
NSLog(@"iOS10 收到本地通知");
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
// 這里做Push跳轉(zhuǎn)的實現(xiàn)
- (void)pushToTargetVCWithdentifier:(NSDictionary *)notifInfoDict {
XSTabBarController *tabVC = (XSTabBarController *)self.window.rootViewController;
tabVC.select = 2;
XSBaseNavigationController *navVC = tabVC.selectedViewController;
//初始化完成,跳轉(zhuǎn)到你想要的目標(biāo)界面
}
接下來分類實現(xiàn),我們是把消息推送在APPDelegate的代碼抽離出來,單獨一個分類處理,這樣APPDelegate中的代碼就不會那么臃腫。創(chuàng)建APPDelegate分類,把消息推送的相關(guān)代碼移動到分類中,但是需要到APPDelegate中的方法時候,我們公開方法出來就可以了,切記不要重寫APPDelegate中的其他公共方法,因為分類的優(yōu)先級大于原來的類, 重新回覆蓋原來的方法,這樣你的公共方法在APPDelegate中就不生效。
推送分類的JPushCategory.h文件實現(xiàn)相關(guān)代碼
#import "AppDelegate.h"
#import "JPUSHService.h"
@interface AppDelegate (JPushCategory)<JPUSHRegisterDelegate>// 成為代理
// 在AppDelegate實現(xiàn)的代碼
/* 注冊遠程推送 */
-(void)setupRemoteNotificationWithlunchInfo:(NSDictionary *)launchOptions;
/* 移除角標(biāo) */
- (void)showBadgeCount ;
@end
之后再推送分類的JPushCategory.m文件實現(xiàn)相關(guān)代碼
#import "AppDelegate+JPushCategory.h"
#import "XSJpushManager.h"
#import "JPUSHService.h"
#import <AdSupport/AdSupport.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#import "EBForeNotification.h"
#import "XSImDBManager.h"
#import "XSBaseNavigationController.h"
#endif
@implementation AppDelegate (JPushCategory)
-(void)setupRemoteNotificationWithlunchInfo:(NSDictionary *)launchOptions {
[self registerNotificationWith:launchOptions]; // 注冊遠程推送
// 如果是點擊消息推送啟動APP
if(receiveReuserInfo && [[NSUserDefaults standardUserDefaults] objectForKey:kXSUserID] ){
[self performSelector:@selector(pus hToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];// 1.0是延時跳轉(zhuǎn),具體的參考你們APP的啟動速度
}
}
- (void)registerNotificationWith:(NSDictionary *)launchOptions{
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption: launchOptions appKey:JPushKey
channel: JPushChannel
apsForProduction: JPushProduction
advertisingIdentifier: nil];
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[JPUSHService registerDeviceToken:deviceToken]; //注冊通知 DeviceToken
}
#pragma mark - iOS7.0 later notification suport
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
[JPUSHService handleRemoteNotification:userInfo];
if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 && application.applicationState >=1 ) {
NSLog(@"ios 7.0 later后臺接收遠程推送");
[self pushToTargetVCWithdentifier:userInfo];
}else{
NSLog(@"ios 7.0 later前臺接收遠程推送");
}
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - JPUSHRegisterDelegate iOS10.0 later suport
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}else {
NSLog(@"iOS10 前臺收到本地通知");
if ([UIApplication sharedApplication].applicationState < 1) {
//NSDictionary *noti = @{@"aps":@{@"alert":notification.request.content.body}};
//[EBForeNotification handleRemoteNotification:noti soundID:1312 isIos10:YES];
}
}
completionHandler(UNNotificationPresentationOptionBadge); // 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以設(shè)置
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的請求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]] ) {
[JPUSHService handleRemoteNotification:userInfo];
[self pushToTargetVCWithdentifier:userInfo]; // 跳轉(zhuǎn)處理
}else{
NSLog(@"iOS10 收到本地通知")
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
#endif
// 消息推送跳轉(zhuǎn)處理
- (void)pushToTargetVCWithdentifier:(NSDictionary *)notifInfoDict {
}
- (void)showBadgeCount{
[JPUSHService setBadge: 0];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; // 極光推送角標(biāo)處理
}
// 在AppDelegate中實現(xiàn)的代碼
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*注冊推送*/
[self setupRemoteNotificationWithlunchInfo:launchOptions];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application cancelAllLocalNotifications];
[self showBadgeCount];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self showBadgeCount];
}
最后,第一寫的比較多,文章也借鑒了很多文章,不過時間太久,找不到鏈接了,在這里謝謝大神們的分享。