1、判断如何启动App
启动App时会自动调用didFinishLaunchingWithOptions方法:如果launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey ,则表示用户是通过点击APNs 启动App;
如果不含有对应键值,则表示用户可能是直接点击icon启动App。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// apn 内容获取:NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]
...
}
2、APNs获取接口
1 ) iOS 6 及以下的系统版本:
如果 App在前台运行收到APNs或者App处于后台运行时点击收到的APNs通知会触发以下接口:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
// apn内容为userInfo
2 ) iOS 7 及以上的系统版本:
如果 App在前台运行收到APNs 或者 App处于后台运行时点击收到的APNs通知 或者(开启了background功能)后台收到background 通知会触发以下接口:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
// apn内容为userInfo
3 )iOS 10及以上的系统版本:
( 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 标准信息内容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
// 取得Extras字段内容
NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服务端中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、补充说明:
1)获取App 运行状态:
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 ()
//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(通知)内容】的基础上修改。