后台模式下的静音通知无法在iOS上使用

时间:2019-01-14 17:55:38

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

我正在使用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": "."}
}

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

1 个答案:

答案 0 :(得分:2)

从OneSignal的API文档来看,您的推送有效载荷应为

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

请注意content_available中的“ _”。