【问题标题】:Silent notification on background mode does not work iOS后台模式下的静默通知不适用于 iOS
【发布时间】:2019-06-08 18:03:31
【问题描述】:

我正在使用 One Signal 接收通知。也是 Xcode 上的 Native Swift 4.2。

我需要向我的应用发送静默通知,只是为了更新数据。我不想打扰用户。

好的,我可以在前台执行此操作。但我不能在后台。

我在 One Signal 网站上测试的一些代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if let aps = userInfo["aps"] as? NSDictionary {
            if let alert = aps["alert"] as? NSDictionary {
                if let body = alert["body"] as? NSString {
                    print(body)
                    if body.isEqual(to: "Pedido") {
                        let strdata = PedidoDAO().getMostRecentDtPedido()
                        // >>>>>>> It works perfectly on FOREGROUND mode, without showing any notification, sound, or banner.**
                        self.loadOrdersFromLastSyncByApi(strdata)
                    }
                }
            }
        }

一些配置:

let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false, kOSSettingsKeyInAppAlerts: false, kOSSettingsKeyInAppLaunchURL: false]

        OneSignal.initWithLaunchOptions(launchOptions,
                                        appId: "yyyyyyyyy-zzzzz-hhhhh-gggg-xxxxxxxxxxxx",
                                        handleNotificationAction: {
                                            (result) in

                                            let payload = result?.notification.payload

                                            if let additionalData = payload?.additionalData {

                                                print("payload")
                                                //  >>>>>>> when app is on BACKGROUND, the notification comes and when I click on it, I can see the additional data. But I dont want to make user click on it.
                                                print(additionalData)
                                            }
                                        },
                                        settings: onesignalInitSettings)

        OneSignal.inFocusDisplayType = OSNotificationDisplayType.none;

        OneSignal.promptForPushNotifications(userResponse: { accepted in
            print("User accepted notifications: \(accepted)")
        })

我也可以在 Postman 上进行测试。它在前台模式下完美运行,但只显示“。”令人讨厌的通知。在背景模式的横幅上:

https://onesignal.com/api/v1/notifications
Headers:
Authorization: Basic MY_REST_API_ONE_SIGNAL
Content-Type: application/json

Body raw:
{
  "app_id": "yyyyyyyyy-zzzzz-hhhhh-gggg-xxxxxxxxxxxx",
  "include_player_ids": ["MY_TEST_IPHONE_ID_"],
  "data": {"Pedido": "000"},
  "content-available" : true,
  "contents": {"en": "."}
}

我不知道是否可以不在后台显示通知,但强制用户点击它来更新数据对我来说没有意义。请问有人有解决方案或其他想法吗?

【问题讨论】:

  • 我已添加功能:后台获取、远程通知和推送通知。
  • 我对 OneSignal 没有任何经验,但如果你不想要 "."作为通知文本,你为什么要发送它?
  • 因为如果我像“”一样发送空,或者没有“内容”行发送,我收到错误:“错误”:“消息通知必须有英文内容”。我不能发送这个空的或不发送它

标签: ios xcode onesignal background-mode silent-notification


【解决方案1】:

从 OneSignal 的 API 文档来看,你的推送负载应该是

{
  "app_id": "yyyyyyyyy-zzzzz-hhhhh-gggg-xxxxxxxxxxxx",
  "include_player_ids": ["MY_TEST_IPHONE_ID_"],
  "data": {"Pedido": "000"},
  "content_available" : true
}

注意content_available中的“_”。

【讨论】:

  • 很好,它有效。不敢相信我没有意识到这一点。我阅读了大约 20 次文档。问题是它不再触发我的附加数据。我将在哪里检查密钥“Pedido”来更新它?
猜你喜欢
  • 1970-01-01
  • 2016-03-05
  • 2017-04-09
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多