在应用未运行iPhone时获取推送通知 [英] get push notification while the app is not running iPhone

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

问题描述

我正在开发一款涉及推送通知的iPhone应用。正如我在许多文档和教程中看到的那样,它建议在

I am working on one iPhone app which involves a push notification. As I have seen in many documents and tutorials it suggests to register for push notification with in

application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

如下:

- (BOOL)application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge|  UIRemoteNotificationTypeSound]; 

    ...
} 

现在的问题是,如果应用程序没有运行(即使在后台),当推送到来时,它无法处理推送消息,但如果我再次从通知区域使用推送消息并再次午餐,我可以收到我的消息。

now the question is, if the app was not running (even in background), when the push come, it cant process the push message, but if I use the push message again from the notification area and lunch the app again, I can get my message.

我需要做些什么来让我的应用程序获得推送消息,即使它第一次吃午饭?

what I need to do to make my app get the push message even when it lunch for the first time?

推荐答案

您可能会混淆注册和接收通知的概念。在第一次调用 registerForRemoteNotificationTypes:方法之前,应用程序无法接收推送通知,因为此方法提供了用于发送推送通知的推送令牌第一个地方。

You might be conflating the notion of registering for and receiving notifications. It is impossible for an app to receive a push notification before the registerForRemoteNotificationTypes: method is called the first time, since this method provides the push token that is used to send push notifications in the first place.

所以,你必须谈论在两个不同的情况下接收通知:在初始应用程序启动时,以及执行期间该程序。

So, you must be talking about receiving notifications under the two separate situations in which they can be delivered: upon initial app launch, and during the execution of the program.

为了处理第一种类型的通知,你必须检查发送给<的选项字典。 code>应用中:didFinishLaunchingWithOptions:。以下代码显示了如何将启动时收到的通知路由到在应用程序运行时推送通知到达时调用的委托方法。

In order to handle notifications of the first type, you must inspect the options dictionary sent to application:didFinishLaunchingWithOptions:. The following code shows how to route a notification received at launch to the delegate method that is called when a push notification arrives while the app is already running.

将此放入您的 application:didFinishLaunchingWithOptions:覆盖:

NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
    [self application:application didReceiveRemoteNotification:pushNotificationPayload];
}

这篇关于在应用未运行iPhone时获取推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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