ios推送的那幾個(gè)方法

方法介紹

( >= IOS 10 )userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

ios10以上調(diào)用,后臺(tái)模式或者應(yīng)用關(guān)閉狀態(tài)。

( >= IOS 10 )userNotificationCenter:willPresentNotification:withCompletionHandler:

ios10以上調(diào)用,應(yīng)用打開狀態(tài)下調(diào)用。

( >= IOS 7 )application:didReceiveRemoteNotification:fetchCompletionHandler:

ios7到ios10之間調(diào)用。在方法內(nèi)部可以判斷[application applicationState]確定應(yīng)用是前臺(tái)、后臺(tái)或者關(guān)閉狀態(tài),然后做出響應(yīng)的響應(yīng)。

( < IOS 7 )application:didReceiveRemoteNotification:

ios7之前調(diào)用,目前ios9以下的用戶不到3%,這個(gè)方法可以放棄了。

友盟推送代碼示例

//
//  AppDelegate+UMeng.m
//  SSWG
//
//  Created by DaLei on 2017/8/29.
//  Copyright ? 2017年 DaLei. All rights reserved.
//

#import "AppDelegate+UMeng.h"
#import "UtilMacros.h"
#import "UMMobClick/MobClick.h"
#import "UMessage.h"
#import <CommonCrypto/CommonDigest.h>//僅作為調(diào)試引用引用的
#import "MessageViewController.h"

@implementation AppDelegate (UMeng)

#pragma mark - application 推送相關(guān)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    DLog(@"application didRegisterForRemoteNotificationsWithDeviceToken");
    //1.2.7版本開始不需要用戶再手動(dòng)注冊(cè)devicetoken,SDK會(huì)自動(dòng)注冊(cè)
    //[UMessage registerDeviceToken:deviceToken];
    NSString *tokenString = [self stringDevicetoken:deviceToken];
    DLog(@"tokenString = %@",tokenString);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
    //關(guān)閉友盟自帶的彈出框
    [UMessage setAutoAlert:NO];
    
    //必須加這句代碼
    [UMessage didReceiveRemoteNotification:userInfo];
    
    DLog(@"%@",userInfo);
    
    UIApplicationState state = [application applicationState];
    //關(guān)閉或者后臺(tái)模式
    if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
        //處理消息跳轉(zhuǎn)
        [self pushNotificationNavigation:userInfo];
        
    } else {//前臺(tái)模式
        
        /////////////////////////////////////////////////////////////////////
        ////////////////////////實(shí)現(xiàn)給tabbar圖標(biāo)增加角標(biāo)////////////////////////
        ////////////////////////////////////////////////////////////////////
    }
}


#pragma mark - 友盟統(tǒng)計(jì)

/**
 友盟統(tǒng)計(jì)功能
 */
- (void)setupUMengStatistic {
    UMConfigInstance.appKey = kAppKey_UMeng;
    UMConfigInstance.channelId = @"App Store";
    //配置以上參數(shù)后調(diào)用此方法初始化SDK!
    [MobClick startWithConfigure:UMConfigInstance];
    //打開調(diào)試模式
    [MobClick setLogEnabled:YES];
}

#pragma mark - 友盟推送

/**
 友盟推送
 */
- (void)setupUMengPushWithOptions:(NSDictionary *)launchOptions{
    
    //設(shè)置AppKey
    [UMessage startWithAppkey:kAppKey_UMeng launchOptions:launchOptions];
    
    //注冊(cè)通知
    [UMessage registerForRemoteNotifications];
    
    //設(shè)置debug模式
    [UMessage openDebugMode:YES];
    
    //iOS10必須加下面這段代碼。
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //點(diǎn)擊允許
        } else {
            //點(diǎn)擊不允許
        }
    }];
    
    //for log
    [UMessage setLogEnabled:YES];
}

#pragma mark UNUserNotificationCenterDelegate

//iOS10新增:處理前臺(tái)收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary *userInfo = notification.request.content.userInfo;
    DLog(@"%@",userInfo);
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //應(yīng)用處于前臺(tái)時(shí)的遠(yuǎn)程推送接受
        
        //關(guān)閉友盟自帶的彈出框
        [UMessage setAutoAlert:NO];
        
        //必須加這句代碼
        [UMessage didReceiveRemoteNotification:userInfo];
        
        /////////////////////////////////////////////////////////////////////
        ////////////////////////實(shí)現(xiàn)給tabbar圖標(biāo)增加角標(biāo)////////////////////////
        ////////////////////////////////////////////////////////////////////
        
    } else {
        //應(yīng)用處于前臺(tái)時(shí)的本地推送接受
        
    }
}

//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    DLog(@"%@",userInfo);
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //應(yīng)用處于后臺(tái)時(shí)的遠(yuǎn)程推送接受
        
        //必須加這句代碼
        [UMessage didReceiveRemoteNotification:userInfo];
        
        //處理消息跳轉(zhuǎn)
        [self pushNotificationNavigation:userInfo];
        
    } else {
        //應(yīng)用處于后臺(tái)時(shí)的本地推送接受
        
    }
}

#pragma mark - 收到通知后的處理頁(yè)面

- (void)pushNotificationNavigation:(NSDictionary *)userInfo{
    
    UIViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
    
    if ([homeVc isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *nav = (UINavigationController *)homeVc;
        
        UIViewController *vc = [nav.viewControllers objectAtIndex:0 ];
        
        MessageViewController *vcnext = MessageViewController.new;
        
        vcnext.view.backgroundColor = [UIColor whiteColor];
        
        [vc.navigationController pushViewController:vcnext animated:YES];
    }
}

#pragma mark - 以下的方法僅作調(diào)試使用

-(NSString *)stringDevicetoken:(NSData *)deviceToken {
    NSString *token = [deviceToken description];
    NSString *pushToken = [[[token stringByReplacingOccurrencesOfString:@"<"withString:@""]
                            stringByReplacingOccurrencesOfString:@">"withString:@""]
                           stringByReplacingOccurrencesOfString:@" "withString:@""];
    return pushToken;
}

-(NSString *)openUDID {
    NSString* openUdid = nil;
    if (openUdid==nil) {
        CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
        CFStringRef cfstring = CFUUIDCreateString(kCFAllocatorDefault, uuid);
        const char *cStr = CFStringGetCStringPtr(cfstring,CFStringGetFastestEncoding(cfstring));
        unsigned char result[16];
        CC_MD5( cStr,(CC_LONG)strlen(cStr), result );
        CFRelease(uuid);
        CFRelease(cfstring);
        openUdid = [NSString stringWithFormat:
                    @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08lx",
                    result[0], result[1], result[2], result[3],
                    result[4], result[5], result[6], result[7],
                    result[8], result[9], result[10], result[11],
                    result[12], result[13], result[14], result[15],
                    (unsigned long)(arc4random() % NSUIntegerMax)];
    }
    return openUdid;
}

@end

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

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

  • 不同版本極光推送SDK集成各有差異,集成時(shí)一定要注意版本號(hào),樓主已將博文更新成最新的SDK JPush v3.0....
    i順頌時(shí)宜閱讀 8,019評(píng)論 37 170
  • 首先有關(guān)于證書的問題我們這里就不一一討論了,證書有關(guān)的可以在百度上一找一大把的,就是需要注意將環(huán)境區(qū)分開就可以了。...
    yezhouxiang閱讀 802評(píng)論 0 0
  • 首先用cocoapods導(dǎo)入 pod 'JPush' 然后在AppDelegate引用 // 引入JPush功能所...
    MoneyLee閱讀 2,187評(píng)論 0 1
  • 應(yīng)用程序必須進(jìn)行適當(dāng)配置,才可以接受本地或遠(yuǎn)程通知。配置過程在iOS和OS X略有不同,但基本原理是相同的。在啟動(dòng)...
    shenzhenboy閱讀 1,472評(píng)論 1 2
  • 橫豎屏切換生命周期方法:執(zhí)行的順序一致,且只執(zhí)行一遍。不會(huì)出現(xiàn)網(wǎng)上說的橫屏切到豎屏執(zhí)行兩遍的問題 一,默認(rèn)情況 a...
    欠兒不登閱讀 3,938評(píng)論 0 3

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