当应用程序未运行时获取推送通知 iPhone
我正在开发一款涉及推送通知的 iPhone 应用程序。正如我在许多文档和教程中看到的那样,它建议注册推送通知,如下
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
所示:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
...
}
现在的问题是,如果应用程序没有运行(即使在后台),当推送来时,它无法处理推送消息,但是如果我再次从通知区域使用推送消息并再次启动应用程序,我就可以收到消息。
我需要做什么才能让我的应用程序即使在第一次午餐时也能收到推送消息?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会混淆注册和接收通知的概念。应用程序不可能在第一次调用
registerForRemoteNotificationTypes:
方法之前接收推送通知,因为此方法首先提供用于发送推送通知的推送令牌。因此,您必须谈论在可以传递通知的两种不同情况下接收通知:初始应用程序启动时和程序执行期间。
为了处理第一种类型的通知,您必须检查发送到
application:didFinishLaunchingWithOptions:
的options
字典。以下代码演示如何将启动时收到的通知路由到应用程序已在运行时推送通知到达时调用的委托方法。将其放入您的
application:didFinishLaunchingWithOptions:
覆盖中: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 toapplication: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: