简体   繁体   English

在后台接收推送通知

[英]Receiving Push Notifications while in background

I know this is covered in a lot of places, but I cannot figure this out. 我知道很多地方都有这个,但我无法弄清楚这一点。 I use Urban Airship for push notifications. 我使用Urban Airship进行推送通知。 Everything seems to be fine except that when my app is in the background - didReceiveRemoteNotification is not getting called. 一切似乎都很好,除了当我的应用程序在后台时 - didReceiveRemoteNotification没有被调用。 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. 但是在后台,它发送的消息 - iOS显示警报 - 未调用didReceiveRemoteNotification 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? 有任何想法吗?

application:didReceiveRemoteNotification: will call in the background only when you have added content-available key with value 1 into the notification payload. application:didReceiveRemoteNotification:仅当您在通知有效负载中添加值为1 content-available密钥时才会在后台调用。 In case of the Urban Airship, you can send Test Push under the Setting tab. 如果是Urban Airship,您可以在Setting选项卡下发送Test Push。 Sample Payload for Push Notifications: 推送通知的示例有效负载:

{
  "aps": {
    "alert": "aaaa",
    "badge": "+1",
    "content-available": "1"
  },
  "device_tokens": [
    "86BA71E361B849E8312A7B943BA6B26A74AB436381CF3FEE3CD9EB436A12A292"
  ]
}

Apple has clearly mentioned in his documentation.... Apple在他的文档中明确提到....

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. 对于触发下载操作的推送通知,通知的有效负载必须包含内容可用密钥,其值设置为1.当存在该密钥时,系统会在后台唤醒应用程序(或将其启动到后台)并且调用app delegate的应用程序: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 https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

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); 如果点击操作按钮(在运行iOS的设备上),系统将启动应用程序,应用程序将调用其委托的应用程序:didFinishLaunchingWithOptions:method(如果已实现); 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的设备上轻触应用程序图标,则应用程序会调用相同的方法, 但不会提供有关通知的信息

I believe the last sentence describes your case, and explains why your application gets no information about the notification. 我相信最后一句话描述了您的情况,并解释了为什么您的应用程序没有获得有关通知的信息。

didReceiveRemoteNotification is calling ONLY if app in foreground or if app is just launched or is bought from background to foreground didReceiveRemoteNotification仅在前台应用程序或应用程序刚刚启动或从后台购买到前台时调用

link in Apple and some question Apple中的链接和一些问题

Method didFinishLaunchingWithOptions:(NSDictionary *)launchOptions parameter launchOptions one of the dictionary keys are UIApplicationLaunchOptionsRemoteNotificationKey which 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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM