当用户点击远程通知时,会在app delegate中触发以下回调:
-application:didReceiveRemoteNotification:fetchCompletionHandler:
在此方案中,应用程序已启动,应用程序状态为UIApplicationStateActive
,我将其解释为用户对远程通知执行操作。
问题: 当远程通知到达并且app处于前景且处于非活动状态时,也可以调用此方法。
示例 :当通知中心视图打开时(从屏幕上边缘向下滑动)或UIAlert打开。在这两种情况下,应用程序状态都是UIApplicationStateActive
,并且无法判断它是否是收到的用户操作通知或系统推送。
问:如何确定didReceiveRemoteNotification
回调是否响应用户点击远程通知与远程通知的到达?
答案 0 :(得分:1)
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
//When your app is in foreground and it received a push notification
}
else if (state == UIApplicationStateInactive)
{
//When your app was in background and it received a push notification
}
此外,如果应用程序未运行且用户点击了通知,则会调用didFinishLaunchingWithOptions。我没有尝试过,但我可以假设您可以从选项中获取通知详细信息。
答案 1 :(得分:0)
要区分didReceiveRemoteNotification中的两个调用,您可以从下面添加此代码。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@“inactivePush" object:nil];
}
else if([UIApplication sharedApplication].applicationState==UIApplicationStateActive){
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
//When the app is in the background
else {
}//End background
}
}