1

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
asr
  • 139
  • 3
  • 11
  • I have added Capabilities: Background fetch, Remote Notifications and Push Notifications. – asr Jan 14 '19 at 18:01
  • 1
    I have no experience with OneSignal whatsoever, but if you don't want "." as the notification text, why are you sending it? – Gereon Jan 14 '19 at 18:40
  • Because if I send empty like "", or send it without the line "contents", I get the error: "errors": "Message Notifications must have English language content". I cannot send this empty or not sending it – asr Jan 14 '19 at 19:05

1 Answers1

2

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.

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • Good, it works. Cant believe I didnt realize this. I read the documentation about 20 times. The problem is that it doesnt trigger my additionalData anymore. Where will I check the key "Pedido" to update it? – asr Jan 14 '19 at 20:10