Reputation: 31
When I run my app from Xcode everything works fine. I get the handleNotificationReceived callback is called and I can read the data from the notification and handle it depending on the data. If I click on a notification the handleNotificationAction callback is called and the same here. If I then minimize the app it works the same - callbacks are called and I can handle the notification.
The problems begin when I terminate the app from the iPhone through the recent app menu and start it from the icon on the desktop. If the app is in the foreground everything works fine like when I start the app from Xcode. When I minimize it I still get the notification but the handleNotificationReceived callback doesn't get called anymore. If I click on a notification the app comes into foreground and then handleNotificationAction is called and shortly after that handleNotificationReceived is also called. As long as the app is in foreground it continues working fine and the callbacks get called. As soon as I minimize the app again handleNotificationReceived doesn't get called anymore.
If I attach the debugger everything starts to work fine again.
Why isn't it called? I am receiving some data in the notifications that I have to save in Core Data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
AppDelegate.configOneSignal(launchOptions)
return true
}
class func configOneSignal(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Void {
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
OneSignal.initWithLaunchOptions(launchOptions,
appId: "MY_APP_ID",
handleNotificationReceived: { notification in
print("notification received")
AppDelegate.handleNotification(notification)
},
handleNotificationAction: { (result) in
print("notification action")
AppDelegate.handleNotification(result?.notification)
},
settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
}
Xcode version: 10.1 Tested iOS versions: 10, 11, 12
Upvotes: 0
Views: 582
Reputation: 11
handleNotificationReceived is triggered in background only if you set in the body the following flags (see doc, under section-content-language):
With the above two flags in the body of the POST, the OS wakes up your application in order to set the state as BACKGROUND. When you application is marked with these state, the handler is triggered.
You can test through Postman.
{ "app_id" : "<your one signal app id>", "included_segments" : ["All"], -> or whatever you want "content_available": "1", "mutable_content": "1", "data": {
"custom_key" : "custom_value" }, "contents": {"en":"notification title"} }
Don't forget to set the headers (Content-Type & Authorization)
Here is an example of usage.
Upvotes: 1