对于高于iOS 10版本的系统,在UNUserNotificationCenterDelegate中,有两种方法可以确定APNS消息方法是从用户单击的地方调用的,还是系统收到消息的:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
而对于iOS 10版本下的系统,应用程序委托方法- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo存在问题,有3种方法可以调用此方法:
2:点击App图标App是否在后台运行或被终止,在App真正启动之前(大约1~3秒),在这几秒内,如果收到APNS消息,也会调用该方法。
3: App正在运行,拉下通知栏使应用程序处于非活动状态,在此状态下如果收到APNS消息,也会调用此方法。
一般情况下,如果用户没有点击消息进入app (点击2次和3次),app不应该执行此消息(如根据消息信息打开一个新页面)。
从下面的答案中,发现kind-2已经用applicationState解决了,而kind-3仍然没有留下答案,有人知道如何修复它吗?
发布于 2018-04-03 17:47:39
您可以使用UIApplication状态进行检查,如下所示:
///- if app is running in foreground
if (application.applicationState == UIApplicationStateActive) {
[self pushNotificationReceivedWhileActiveWithInfo:userInfo];
///- if app was running in background
} else if (application.applicationState == UIApplicationStateBackground) {
if (self.pushReceivedWhileClosed != YES) {
[self pushNotificationReceivedWhileInBackgroundWithInfo:userInfo];
}
}此外,您还需要在应用程序完全关闭时处理通知,从通知中打开时从didFinishLaunchingWithOptions获取通知,保存通知数据并在用户到达主屏幕时执行通知。
///- when receiving push notification while [app closed]
if (launchOptions != nil) {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil) {
NSLog(@"[AppDelegate]##########################AppDelegate######################### Received notification while app CLOSED");
///- save the dictionary data and execute it when the app loads for example.
self.pushReceivedWhileClosed = YES;
}
}发布于 2018-04-03 17:52:01
在应用程序didReceiveRemoteNotification中检查应用程序状态
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//app was on background
}
}发布于 2018-04-03 18:50:56
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
comingFromNotification = YES;
}else{
comingFromNotification = NO;
}
}https://stackoverflow.com/questions/49626543
复制相似问题