iOS OneSignal handleNotificationReceived回调在应用程序重新启动后未调用



当我从Xcode运行我的应用程序时,一切都很好。我得到handleNotificationReceived回调被调用,我可以从通知中读取数据并根据数据进行处理。如果我单击通知,将调用handleNotificationAction回调,此处也是如此。如果我最小化应用程序,它的工作原理相同——调用回调,我可以处理通知。

当我通过最近的应用程序菜单从iPhone上终止应用程序并从桌面上的图标启动它时,问题就开始了。如果应用程序在前台,一切都很好,就像我从Xcode启动应用程序一样。当我最小化它时,我仍然会收到通知,但handleNotificationReceived回调不再被调用。如果我点击通知,应用程序将进入前台,然后调用handleNotificationAction,不久之后还会调用handleNertificationReceived。只要应用程序处于前台,它就会继续正常工作,并调用回调。一旦我再次最小化应用程序,handleNotificationReceived就不再被调用。

如果我附加调试器,一切都会重新开始正常工作。

为什么不叫它?我在通知中收到一些数据,我必须保存在核心数据中。

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版本:10.1经过测试的iOS版本:10、11、12

handleNotificationReceived只有在正文中设置了以下标志时才会在后台触发(请参阅文档中部分内容语言下(:

  • 可用内容:1
  • 可变内容:1

POST正文中有了以上两个标志,操作系统就会唤醒您的应用程序,以便将状态设置为"背景"。当应用程序标记有这些状态时,将触发处理程序。

你可以通过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"} }

不要忘记设置标题(内容类型和授权(

下面是一个用法示例。

最新更新