当应用程序在后台被用户杀死时,正在接收推送,当用户单击推送通知时,它会打开应用程序,并且不会崩溃,但会立即关闭。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
print("The userInfo is: \(userInfo)")
completionHandler(UIBackgroundFetchResult.newData)
}
以前的方法甚至都没有调用,或者也许是,但是由于应用程序未运行,我不知道如何调试它。
这是我通过服务器发送的通知对象:
apns: {
payload: {
aps: {
alert: {
title: messageObject.authorName,
body: messageObject.content,
},
sound: "notification.wav",
category: "ChatViewController"
}
}
}
这就是通过服务器发送的内容。当应用程序在后台但未被杀死时,或者在前景中时,通知才起作用。
答案 0 :(得分:0)
我认为,当用户在您的应用程序被杀或处于后台或前台时单击通知时,将执行以下方法。您必须一步一步地对其进行测试。
首先,当您单击通知时,请检查您是否能够在应用程序运行后立即看到弹出窗口。只需复制此代码,并将其粘贴到您的 AppDelegate 类中,然后注释您的 didReceiveRemoteNotification 方法,然后检查应用程序是否崩溃。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
guard let object = response.notification.request.content.userInfo as? [String : AnyObject] else {
return
}
self.normalPopup()
}
/// Alert Popup, if user not exist in our database.
func normalPopup() {
let alert = UIAlertController(title: "Test", message: "Show pop up window.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
}))
self.window?.rootViewController?.present(alert, animated: true)
}
如果显示弹出窗口,请注释 normalPopup 函数,并用下面的代码替换该函数。然后检查应用程序被杀死时是否崩溃了。
Messaging.messaging().appDidReceiveMessage(userInfo)
请让我知道您是否仍然面临同样的问题。
答案 1 :(得分:0)
如果首先使用firebase,则应在application(_ application:, didFinishLaunchingWithOptions:)
上对其进行定义,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
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()
Messaging.messaging().delegate = self
FirebaseApp.configure()
return true
}
使用此功能,您还可以在应用运行时处理通知:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
和
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
,您应该使用以下方法将新令牌设置为“消息传递”:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
let token = deviceToken.map { String(format: "%02.2hhx", $0)}.joined()
print(token)
}
最后,您可以处理具有以下扩展名的通知
// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
//*************************
// Handle push notification message automatically when app is running
//*************************
print(userInfo)
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
//*************************
// Handle push notification message after click on it
//*************************
print(userInfo)
completionHandler()
}
}