flutter 无法监听到消息通知
当有消息到到达时,一直提示 “UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.”。
我的的 AppDelegate.swift如下
import UIKit
import Flutter
import umeng_push_sdk;
import UserNotifications;
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//iOS10新增:处理前台收到通知的代理方法
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if notification.request.trigger is UNPushNotificationTrigger {
//应用处于前台时的远程推送接受
NSLog("-----------willPresentNotification");
//关闭友盟自带的弹出框
UMessage.setAutoAlert(false)
//必须加这句代码
UMessage.didReceiveRemoteNotification(userInfo)
} else {
//应用处于后台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptions(rawValue: UNNotificationPresentationOptions.RawValue(
UInt8(UNNotificationPresentationOptions.sound.rawValue) | UInt8(UNNotificationPresentationOptions.badge.rawValue) | UInt8(UNNotificationPresentationOptions.alert.rawValue))))
}
//iOS10新增:处理后台点击通知的代理方法
@available(iOS 10.0, *)
override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if response.notification.request.trigger is UNPushNotificationTrigger {
//应用处于前台时的远程推送接受
//关闭友盟自带的弹出框
UMessage.setAutoAlert(false)
//必须加这句代码
UMessage.didReceiveRemoteNotification(userInfo)
} else {
//应用处于后台时的本地推送接受
}
}
}

