【问题标题】:how to detect user clicked iOS remote notification?如何检测用户点击的 iOS 远程通知?
【发布时间】:2015-10-03 01:00:00
【问题描述】:

当用户点击远程通知时,会在应用委托中触发以下回调:

-application:didReceiveRemoteNotification:fetchCompletionHandler:

在这种情况下,应用程序已启动,应用程序状态为 UIApplicationStateActive,我将其解释为用户对远程通知进行了操作。

问题: 当远程通知到达并且应用处于前台处于非活动状态时,也可​​以调用此方法。

示例:当通知中心视图打开(从屏幕顶部边缘向下滑动)或 UIAlert 打开时。在这两种情况下,应用程序状态都是UIApplicationStateActive,并且无法判断它是用户操作的通知还是收到的系统推送。

:如何确定didReceiveRemoteNotification 回调是对用户点击远程通知还是远程通知到达的响应?

【问题讨论】:

    标签: ios objective-c iphone push-notification apple-push-notifications


    【解决方案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。我没有尝试过,但我可以假设您可以从选项中获取通知详细信息。

    【讨论】:

    • tx 回复,这是一个不同的问题。当应用程序处于前台并且通知中心打开时,应用程序处于非活动状态。现在当didReceiveRemoteNotification 被调用时,可能有两种情况: 1-用户对通知中心通知的操作 2-新推送通知的到来。问题是没有办法区分这两种情况。
    【解决方案2】:

    要区分 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
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多