1. App关闭时接收到他推送通知,通过点击推送通知来启动App
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
在程序启动完成后,在此方法中可以得到推送通知的类容,此处又分为远程通知和本地通知的区别。
远程通知
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
本地通知
UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSDictionary * userInfo = localNoti.userInfo;
这里,为了让主界面先加载完成,一般需要延迟一小段时间后再去处理推送通知,push出相应的响应页面等。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self didReceiveNotification:userInfo];
});
2.App正在运行时接收到推送通知
远程通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
App状态
UIApplicationStateActive, // 激活状态,用户正在使用App
UIApplicationStateInactive, // 不激活状态,用户切换到其他App、按Home键回到桌面、拉下通知中心
UIApplicationStateBackground // 在后台运行
根据application.applicationState的状态,判断执行哪种动作。
网友评论