当我的应用程序处于非活动状态时发送远程推送通知时,将显示警报,但未触发完成块中的其余代码(对“ doSomething()”的调用)。在打开应用程序时会触发,但我仍然无法弄清楚为什么在提示仍然显示的情况下,当应用程序处于非活动状态时它不会显示。
我将Firebase用于后端
我的willPresent方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
doSomething("willPresent notification")
// Change this to your preferred presentation option
completionHandler([.alert])
}
doSomething():
func doSomething(_ senderMethod: String) {
if let userId = Auth.auth().currentUser?.uid {
let userRef = Database.database().reference().child("Users").child(userId)
let someValue = ["sent": senderMethod] as [String : AnyObject]
userRef.updateChildValues(someValue)
}
}
JSON有效载荷:
{
"to": deviceId,
"notification": {
"body": "test body",
"title": "Test",
"sound": "default"
},
"priority": "high"
}
didFinishLaunching:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
setPushNotifications(application)
return true
}
func setPushNotifications(_ application: UIApplication) {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}