iOS:推送通知仅在应用程序在前台运行时才起作用
我有 2 部 iPhone,正在测试 Apple 推送通知。
当我的应用程序在两个设备上的前台运行时,它们完美地工作。但如果我通过单击“主页”按钮将其关闭,我将不再收到通知。
系统不显示通知弹出窗口。我认为这并不取决于我的应用程序代码...
到目前为止,我已经实现了
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
,并且当应用程序位于前台时效果很好。 我也实现了:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
但 launchOptions 从未包含通知。但是,我想这是正确的,因为当我选择我从未见过的通知警报按钮时,会调用最后一个方法...
谢谢
I have 2 iPhones and I'm testing Apple push notifications.
They perfectly work when my app is running in foreground on both devices. But if I close it, by clicking on the Home button, I don't receive the notifications anymore.
The system doesn't show the notification popup. And I think this doesn't depend on my app code...
So far I've implemented
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
and it works great when the app is in foreground.
I've also implemented:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
but the launchOptions never contain the notification. However, this is correct, I guess, because the last method is invoked when I select a button of a notification alert that I never see...
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用方法
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
时,您应该指示适当的参数类型。如果您想在系统收到设备的推送通知时显示警报,那么您应该传递UIRemoteNotificationTypeAlert
。您还可以组合类型并将其传递到该参数中:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeSound UIRemoteNotificationTypeBadge。
When you call method
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
you should indicate appropriate type of argument. If you want to display alert when system receive push-notification for your device then you should passUIRemoteNotificationTypeAlert
.Also you can combine types and pass it in that argument:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge
.