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

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

问题描述

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

中注册推送通知

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

如下:

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

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

我需要做什么才能让我的应用在第一次吃午饭时收到推送消息?

解决方案

您可能会混淆注册和接收通知的概念.应用程序不可能在第一次调用 registerForRemoteNotificationTypes: 方法之前收到推送通知,因为该方法首先提供了用于发送推送通知的推送令牌.>

因此,您必须谈论在两种不同的情况下接收通知,它们可以传送:应用程序初始启动时和程序执行期间.

为了处理第一种类型的通知,您必须检查发送到 application:didFinishLaunchingWithOptions:options 字典.以下代码展示了如何将在启动时收到的通知路由到在应用已经运行时推送通知到达时调用的委托方法.

把它放在你的application:didFinishLaunchingWithOptions: override:

NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];如果(推送通知有效载荷){[自申请:申请 didReceiveRemoteNotification:pushNotificationPayload];}

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

like the following:

- (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?

解决方案

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.

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.

Place this in your application:didFinishLaunchingWithOptions: override:

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

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

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