asr
asr

Reputation: 169

Silent notification on background mode does not work iOS

I am using One Signal to receive notifications. Also Native Swift 4.2 on Xcode.

I need to send silent notifications to my app, just to update data. I don't want to bother the user.

Ok, I can do that on foreground. But I cannot on background.

Some of my code that I am testing on One Signal website:

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)
                    }
                }
            }
        }

Some configuration:

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)")
        })

I can also test it on Postman. It works perfectly on foreground mode, but showing annoying notification with only "." on banner on background mode:

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": "."}
}

I dont know if it is possible to NOT show the notification on background, but it doesnt make sense for me to force user click on it to update data. Does anyone have a solution or another idea, please?

Upvotes: 1

Views: 1649

Answers (1)

Gereon
Gereon

Reputation: 17844

Judging from OneSignal's API documentaton, your push payload should be

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

Note the "_" in content_available.

Upvotes: 2

Related Questions