在后台时 didReceiveRemoteNotification [英] didReceiveRemoteNotification when in background

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

问题描述

此类问题已被问过多次,但我遇到了一些具体情况.

These kind of question has been asked a number of times but i have some specific situation going on.

当我的应用程序处于活动状态并且我收到一条 PUSH 消息时,我能够成功解析自定义负载等.

When my application is active and I receive a PUSH message i'm successfully able to parse the custom payloads and such.

但是,当我的应用程序在后台并且 PUSH 到达时,用户必须单击查看/打开"按钮才能调用 didReceiveRemoteNotificationdidFinishLaunchingWithOptionscode> 在那之后被调用.

However when my application is in the background and the PUSH arrives the user has to click on the 'View/Open' button in order to get the didReceiveRemoteNotification called and the didFinishLaunchingWithOptions is called after that.

我需要让我的应用程序决定是否必须在后台使用 UIAlert 提示用户,或者根据某些本地设置禁止推送消息.

I need to have my application decide if they have to prompt the user with an UIAlert when in the background or suppress the push message based on some local settings.

任何帮助将不胜感激,

推荐答案

你的应用需要处理所有可能的推送通知传递状态:

You app needs to handle all the possible push notification delivery states:

  • 您的应用刚刚发布

  • Your app was just launched

您的应用刚刚从后台带到前台

Your app was just brought from background to foreground

您的应用已经在前台运行

Your app was already running in the foreground

您无法在交付时选择用于呈现推送通知的呈现方法,即在通知本身中编码(可选警报、徽章编号、声音).但是,由于您可能同时控制了应用程序和推送通知的有效负载,因此您可以在有效负载中指定是否已经向用户呈现了警报视图和消息.只有在应用已经在前台运行的情况下,您才知道用户不是通过警报或定期从主屏幕启动您的应用.

You do not get to choose at delivery time what presentation method is used to present the push notification, that is encoded in the notification itself (optional alert, badge number, sound). But since you presumably are in control of both the app and the payload of the push notification, you can specify in the payload whether or not there was an alert view and message already presented to the user. Only in the case of the app is already running in the foreground do you know that the user did not just launch your app through an alert or regularly from the home screen.

您可以使用以下代码在 didReceiveRemoteNotification 中判断您的应用是否刚刚进入前台:

You can tell whether your app was just brought to the foreground or not in didReceiveRemoteNotification using this bit of code:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app was already in the foreground
    else
        // app was just brought from background to foreground
    ...
}

这篇关于在后台时 didReceiveRemoteNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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