方法介紹
( >= 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