首页 > 解决方案 > Flutter 中的 One Signal 静默通知

问题描述

我为两个平台实现了静默通知。根据 OneSignal 的文档,在 Android 上我必须添加一个类 NotificationServiceExtender 而在 IOS 中我只需要从 REST API content_available:true 发送

正常通知在两个平台上都有效,但 IOS 中的静默通知没有触发:

   OneSignal.shared
        .setNotificationReceivedHandler((OSNotification notification) async {
     print(notification);
}

在 AppDelegate 中,我测试了是否从 OneSignal 返回任何内容,并且似乎正在返回数据:

  override func application(_ application: UIApplication, didReceiveRemoteNotification 
userInfo: [AnyHashable : Any]) {
 print(userInfo)
    } 

我不知道 OneSignal 的 FLutter 上是否有另一种可以触发的方法,或者我应该建立一个到 IOS 的通道并检索数据。这样可能会变得复杂。

另外我必须提到,静默通知正在 Android 设备上运行。

包裹: onesignal_flutter: ^2.0.0

标签: iosflutteronesignal

解决方案


我没有找到直接检索收到的通知的方法。因此,我创建了一个到 Native IOS 的 Flutter Channel,我可以从中检索数据。

override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    var notificationData:NSMutableDictionary
    var isAppInFocus: Bool = false

                   if let custom = userInfo["custom"] as? NSDictionary {
                        if let data = custom["a"] as? NSDictionary {
                            notificationData = data.mutableCopy() as! NSMutableDictionary

                            if(data["category"] as? String == "MESSAGESTATUS"){
                                notificationChannel.invokeMethod("getSilentLastSeenMessage", arguments: notificationData)
                            }

                    }}
    }

}


推荐阅读