極光推送精準(zhǔn)跳轉(zhuǎn)及簡化AppDelegate代碼(分類方法)

之前項目集成極光推送,遇到了一些問題,現(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];
}

最后,第一寫的比較多,文章也借鑒了很多文章,不過時間太久,找不到鏈接了,在這里謝謝大神們的分享。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,765評論 25 709
  • 推送通知注意:這里說的推送通知跟NSNotification有所區(qū)別NSNotification是抽象的,不可見的...
    醉葉惜秋閱讀 1,611評論 0 3
  • 極光推送: 1.JPush當(dāng)前版本是1.8.2,其SDK的開發(fā)除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,873評論 10 16
  • 說一下最近項目里遇到的極光推送: 具體步驟: 一:首先需要一個推送的p12 證書:我是傳送門(創(chuàng)建推送證書) 但是...
    9e5f2143c765閱讀 816評論 0 0

友情鏈接更多精彩內(nèi)容