1迅脐、判斷如何啟動App
啟動App時會自動調(diào)用didFinishLaunchingWithOptions方法:如果launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey ,則表示用戶是通過點擊APNs 啟動App谴蔑;
如果不含有對應(yīng)鍵值龟梦,則表示用戶可能是直接點擊icon啟動App窃躲。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// apn 內(nèi)容獲鹊僦稀:NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]
...
}
2、APNs獲取接口
1 ) iOS 6 及以下的系統(tǒng)版本:
如果 App在前臺運行收到APNs或者App處于后臺運行時點擊收到的APNs通知會觸發(fā)以下接口:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
// apn內(nèi)容為userInfo
2 ) iOS 7 及以上的系統(tǒng)版本:
如果 App在前臺運行收到APNs 或者 App處于后臺運行時點擊收到的APNs通知 或者(開啟了background功能)后臺收到background 通知會觸發(fā)以下接口:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
// apn內(nèi)容為userInfo
3 )iOS 10及以上的系統(tǒng)版本:
( 1 ) 和( 2 ) 點說到的通知獲取方法由以下兩個代理方法替代:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
__IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
__IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED;
3挣输、示例代碼:
// iOS 6 Remote Notificatio
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// 取得 APNs 標(biāo)準(zhǔn)信息內(nèi)容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內(nèi)容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge數(shù)量
NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音
// 取得Extras字段內(nèi)容
NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服務(wù)端中Extras字段撩嚼,key是自己定義的
NSLog(@"content =[%@], badge=[%d], sound=[%@], customize field =[%@]",content,badge,sound,customizeField1);
}
//iOS 7 Remote Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
NSLog(@"this is iOS7 Remote Notification");
// iOS 10 以下 Required
if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
//iOS10以下的通知處理代碼
return;
}
completionHandler(UIBackgroundFetchResultNewData);
}
//iOS10新增:處理前臺收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"--ios10 官方-前臺收到push通知:%@",userInfo);
}else{
NSLog(@"--ios10 官方-前臺收到本地通知:{\n body:%@,\n title:%@,subtitle:%@,badge:%@,\nsound:%@,\nuserinfo:%@\n}",notification.request.content.body,notification.request.content.title,notification.request.content.subtitle, notification.request.content.badge,notification.request.content.sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
//iOS10新增:處理后臺點擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"--ios10 官方-后臺收到push通知:%@",userInfo);
}else{
NSLog(@"--ios10 官方-后臺收到本地通知:{\n body:%@,\n title:%@,subtitle:%@,badge:%@,\nsound:%@,\nuserinfo:%@\n}",response.notification.request.content.body,response.notification.request.content.title,response.notification.request.content.subtitle, response.notification.request.content.badge,response.notification.request.content.sound,userInfo);
}
completionHandler();
}
4恋技、補(bǔ)充說明:
1)獲取App 運行狀態(tài):
UIApplicationState state = [UIApplication sharedApplication].applicationState;
說明:
UIApplication.h
ypedef NS_ENUM(NSInteger, UIApplicationState) {
UIApplicationStateActive,
UIApplicationStateInactive,
UIApplicationStateBackground
} NS_ENUM_AVAILABLE_IOS(4_0);
2)iOS 10使用代理方法需要在delegate添加代理逻族,如:
//Apple官方 - iOS 10 新增的delegate:
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
//Apple官方 - iOS10 APNs注冊:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
center.delegate = self;
ps:此文檔在JPush官方文檔【獲取APNs(通知)內(nèi)容】的基礎(chǔ)上修改聘鳞。