Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 后台模式下的静默通知不起作用_Ios_Xcode_Onesignal_Background Mode_Silent Notification - Fatal编程技术网

Ios 后台模式下的静默通知不起作用

Ios 后台模式下的静默通知不起作用,ios,xcode,onesignal,background-mode,silent-notification,Ios,Xcode,Onesignal,Background Mode,Silent Notification,我使用一个信号来接收通知。也是Xcode上的本机Swift 4.2 我需要向我的应用程序发送静默通知,只是为了更新数据。我不想打扰用户 好的,我可以在前台做。但我不知道背景 我在一个信号网站上测试的一些代码: func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHan

我使用一个信号来接收通知。也是Xcode上的本机Swift 4.2

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

好的,我可以在前台做。但我不知道背景

我在一个信号网站上测试的一些代码:

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)")
        })
我也可以在邮递员身上测试。它在前台模式下工作得很好,但在后台模式下仅在横幅上显示“.”即可显示恼人的通知:

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的API文档判断,推送负载应该是

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

注意
content\u中的“u”可用

我添加了一些功能:后台获取、远程通知和推送通知。我对OneSignal没有任何经验,但是如果你不想“.”作为通知文本,为什么要发送它?因为如果我发送空的,比如“”,或者发送时没有“contents”行,我得到错误:“错误”:“消息通知必须有英语内容”。我不能发送这个空的或不发送它好,它的工作。真不敢相信我竟然没有意识到这一点。我把文档读了大约20遍。问题是它不再触发我的额外数据。我将在哪里检查“Pedido”键来更新它?