推送通知处理 [英] push notification handling

查看:107
本文介绍了推送通知处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Apple的文档

I'm reading Apple's docs on

在我看来有矛盾的说法.有人可以清除这些混淆点吗?现在,让我们严格来讲远程通知(相对于本地).

and it looks to me to have conflicting statements. Can someone clear up these confusion points? Let's speak strictly of remote notification (versus local) for now.

文档说,如果按下通知上的操作按钮,它将调用application:didFinishLaunchingWithOptions并传入通知有效负载.稍后它说如果应用程序在前台运行,它将通过application:didReceiveRemoteNotification:传递通知.这对我来说意味着,当应用程序后台运行或未运行时,将调用application:didFinishLaunchingWithOptions.否则,将调用application:didReceiveRemoteNotification:.

The docs say that if the action button on the notification is pressed, it calls application:didFinishLaunchingWithOptions and passes in the notification payload. Later it says if the app is running in the foreground, it delivers the notification via application:didReceiveRemoteNotification:. This implies to me that when the app is backgrounded or not running, then application:didFinishLaunchingWithOptions is called. Otherwise, application:didReceiveRemoteNotification: is called.

后来,有一个iOS注释说:

Later, there is an iOS Note saying the following:

"iOS注意:在iOS中,您可以通过检查应用程序状态来确定是否由于用户点击操作按钮而启动了应用程序,还是将通知传递给了已经在运行的应用程序. application:didReceiveRemoteNotification:或application:didReceiveLocalNotification:方法的值,获取applicationState属性的值并对其进行评估;如果值为UIApplicationStateInactive,则用户轻按操作按钮;如果值为UIApplicationStateActive,则应用程序在接收时位于最前面通知."

"iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification."

这对我来说意味着,当应用程序已经前景显示并且用户按下操作按钮(或在iOS 5中滑动操作滑块)以前景/启动应用程序时,都会调用application:didReceiveRemoteNotification:.

This implies to me that application:didReceiveRemoteNotification: is called both when the app is already foregrounded and if the user presses the action button (or slides the action slider in iOS 5) to foreground/launch the app.

我感到困惑的原因可能是文档的第一部分暗示通知有效负载是与以下应用程序一起发送的:didFinishLaunchingWithOptions:方法或对正在运行的"应用程序的误解(是后台应用程序被视为正在运行" ?). application:didReceiveRemoteNotification的文档:指出它被称为正在运行"的应用程序.

The source of my confusion might be with the first portion where the docs imply the notification payload is sent with the application:didFinishLaunchingWithOptions: method or with a misunderstanding of what a "running" application is (is a backgrounded app considered "running"?). The documentation for application:didReceiveRemoteNotification: states it is called for "running" applications.

因此,总而言之,我可以澄清一下吗:

So, to summarize, could I get clarification on:

1)在前台显示应用程序或用户选择对通知执行操作"时,是否总是调用application:didReceiveRemoteNotification :?如果不是,那么我们在确定应用程序状态为活动或非活动状态时如何理解iOS注释?

1) Is application:didReceiveRemoteNotification: always called when the app is foregrounded or when the user selects to "act" on the notification? If not, how do we make sense of the iOS Note on determining the Application State being active or inactive?

2)后台应用程序是否正在运行",至少在声称该应用程序的文档中就这样:正在运行的应用程序会调用didReceiveRemoteNotification?

2) Is a backgrounded app "running", at least in the sense of the docs claiming application:didReceiveRemoteNotification is called for running apps?

3)为完成此操作,后台应用程序UIApplicationStateInactive还是Active?

3) For completion, is a backgrounded app UIApplicationStateInactive or Active?

推荐答案

此处的措词令人困惑,尤其是围绕单词背景的措辞.

The wording here is confusing, especially around the word backgrounding.

当应用程序确实未真正加载到内存中时(例如,启动它时,会显示启动屏幕等),然后调用application:didFinishLaunchingWithOptions,您可以按以下方式获取推送通知:

When the application is truly not loaded in memory (e,g. when you launch it the splash screen shows up etc), then application:didFinishLaunchingWithOptions is called, and you can get the push notification as follows:

NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(remoteNotif)
{
    //Handle remote notification
}

如果该应用程序已加载到内存中并且处于活动状态(例如,该应用程序当前在设备上打开),则仅会调用application:didReceiveRemoteNotification:.

If the app is loaded in memory and is ACTIVE (e.g. the app is currently open on the device) then only application:didReceiveRemoteNotification: is called.

如果应用程序已加载到内存中,但未处于活动状态且未设置背景(例如,您启动了该应用程序,然后按了主页按钮,并等待了10秒钟),然后单击了推送通知上的操作按钮,仅didReceiveRemoteNotification被调用.

If the app is loaded in memory but is not ACTIVE and NOT BACKGROUNDING (e.g., you launched the app, then pressed the home button, and waited 10 seconds), and then you click the action button on a push notification, only didReceiveRemoteNotification is called.

您可以捕获这种情况,如下所示:

You can capture this case as follows:

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if([app applicationState] == UIApplicationStateInactive)
    {
        //If the application state was inactive, this means the user pressed an action button
        // from a notification. 

    //Handle notification
    }
}

这篇关于推送通知处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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