检测应用程序是否:didReceiveRemoteNotification: fetchCompletionHandler: 通过点击通知中心的通知调用 [英] Detect if application: didReceiveRemoteNotification: fetchCompletionHandler: was called by tapping on a notification in Notification Center

查看:22
本文介绍了检测应用程序是否:didReceiveRemoteNotification: fetchCompletionHandler: 通过点击通知中心的通知调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

application: didReceiveRemoteNotification: fetchCompletionHandler:

不同于

application: didReceiveRemoteNotification:

怎么样?来自文档:

与 application:didReceiveRemoteNotification: 方法不同,它是仅在您的应用程序运行时调用,系统调用此方法无论您的应用程序处于何种状态.您的应用是否被暂停运行,系统唤醒或启动您的应用程序并将其放入调用方法前的后台运行状态.如果用户打开您的应用程序从系统显示的警报中,系统调用此方法再次,以便您知道用户选择了哪个通知.

Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your app and puts it into the background running state before calling the method. If the user opens your app from the system-displayed alert, the system calls this method again so that you know which notification the user selected.

我的问题是:我想知道该方法是由用户点击来自通知中心的系统显示警报还是来自唤醒设备的无声推送通知调用的.目前,据我所知,没有明显的区分方法.

My struggle is: I want to know if the method was called by the user tapping an a system-displayed alert from the Notification Center or from a silent push notification that wakes up the device. Currently, as far as I can see, there is no obvious way to differentiate.

- (BOOL)application: didFinishLaunchingWithOptions:

跟踪上述方法中的 launchOptions 不是解决方案,因为它仅在应用程序暂停/不在后台运行时才被调用.如果它在后台运行,则不会被调用.

Tracking the launchOptions in the above method is not a solution because it's only called if the app is suspended/not running in background. If it's running in the background it doesn't get called.

推荐答案

Apple 文档有点混乱

The Apple docs are a bit confusing

application: didReceiveRemoteNotification: fetchCompletionHandler:  

如果您的应用程序支持远程通知后台模式(即您正在执行 BackgroundFetch),则使用.

is used if your application supports the remote-notification background mode (ie you're doing BackgroundFetch.)

application: didReceiveRemoteNotification:  

当操作系统收到 RemoteNotification 并且应用程序正在运行(在后台/挂起或前台)时调用.
您可以检查 UIApplicationState 以查看应用是否被用户带到前台(点击通知)或在通知进入时已在运行.

is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
You can check the UIApplicationState to see if the app was brought to foreground by the user (tapping on notification) or was already running when notification comes in.

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
      UIApplicationState state = [application applicationState];
        // user tapped notification while app was in background
    if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
         // go to screen relevant to Notification content
    } else {
         // App is in UIApplicationStateActive (running in foreground)
         // perhaps show an UIAlertView
    }
}

这篇关于检测应用程序是否:didReceiveRemoteNotification: fetchCompletionHandler: 通过点击通知中心的通知调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆