ios 在后台接收推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14616261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Receiving Push Notifications while in background
提问by Brandon
I know this is covered in a lot of places, but I cannot figure this out. I use Urban Airship for push notifications. Everything seems to be fine except that when my app is in the background - didReceiveRemoteNotification
is not getting called. It works when in the foreground - I am able to properly process messages. And I am able to get to messages from launch options if message is tapped from notifications center. But when in the background, a message it send - iOS displays the alert - didReceiveRemoteNotification
is not called. When I tap on my Application icon (not from within notification center) the app comes to the foreground and I have no idea the notification is present. Any ideas?
我知道这在很多地方都有涉及,但我无法弄清楚。我使用 Urban Airship 进行推送通知。一切似乎都很好,除了当我的应用程序在后台时 -didReceiveRemoteNotification
没有被调用。它在前台工作 - 我能够正确处理消息。如果从通知中心点击消息,我可以从启动选项获取消息。但是在后台时,它发送的消息 - iOS 显示警报 -didReceiveRemoteNotification
不会被调用。当我点击我的应用程序图标(不是从通知中心内)时,应用程序会出现在前台,我不知道通知是否存在。有任何想法吗?
采纳答案by Eran
From the APNS programming guide :
来自 APNS 编程指南:
Let's review the possible scenarios when the operating delivers a local notification or a remote notification for an application.
The notification is delivered when the application isn't running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate's application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification.
让我们回顾一下操作为应用程序传递本地通知或远程通知时可能出现的场景。
当应用程序不在前台运行时,会传递通知。在这种情况下,系统会显示通知、显示警报、标记图标,或者播放声音。
作为呈现通知的结果,用户点击警报的操作按钮或点击(或点击)应用程序图标。如果点击操作按钮(在运行 iOS 的设备上),系统启动应用程序,应用程序调用其委托的 application:didFinishLaunchingWithOptions: 方法(如果已实现);它传入通知负载(用于远程通知)或本地通知对象(用于本地通知)。
如果在运行 iOS 的设备上点击应用程序图标,应用程序将调用相同的方法,但不提供有关通知的信息。
I believe the last sentence describes your case, and explains why your application gets no information about the notification.
我相信最后一句话描述了您的情况,并解释了为什么您的应用程序没有获取有关通知的信息。
回答by Muhammad Ali Yousaf
application:didReceiveRemoteNotification:
will call in the background only when you have added content-available
key with value 1
into the notification payload.
In case of the Urban Airship, you can send Test Push under the Setting tab.
Sample Payload for Push Notifications:
application:didReceiveRemoteNotification:
仅当您将content-available
带值的键添加1
到通知负载时才会在后台调用。如果是 Urban Airship,您可以在设置选项卡下发送测试推送。推送通知的示例负载:
{
"aps": {
"alert": "aaaa",
"badge": "+1",
"content-available": "1"
},
"device_tokens": [
"86BA71E361B849E8312A7B943BA6B26A74AB436381CF3FEE3CD9EB436A12A292"
]
}
Apple has clearly mentioned in his documentation....
苹果在他的文档中明确提到......
For a push notification to trigger a download operation, the notification's payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate's application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
对于触发下载操作的推送通知,通知的有效负载必须包含 content-available 键,其值设置为 1。当该键存在时,系统会在后台唤醒应用程序(或将其启动到后台)和调用应用程序委托的 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法。您对该方法的实现应下载相关内容并将其集成到您的应用程序中。 https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
回答by CReaTuS
didReceiveRemoteNotification is calling ONLY if app in foreground or if app is just launched or is bought from background to foreground
didReceiveRemoteNotification 仅在应用程序处于前台或应用程序刚刚启动或从后台购买到前台时才调用
link in Appleand some question
Apple 中的链接和一些问题
回答by Mantas Laurinavi?ius
Method didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsparameter launchOptions one of the dictionary keys are UIApplicationLaunchOptionsRemoteNotificationKeywhich holds the pressed push notification info.
方法didFinishLaunchingWithOptions:(NSDictionary *)launchOptions参数 launchOptions 字典键之一是UIApplicationLaunchOptionsRemoteNotificationKey,它保存按下的推送通知信息。
You can push received info after tour main root controller is initialised. I save it to some property and then push it after view is initialised.
您可以在游览主根控制器初始化后推送收到的信息。我将它保存到某个属性,然后在视图初始化后推送它。
if (launchOptions) {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
self.notificationToMakeAction = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
}
}