didReceiveRemoteNotification函数在FCM通知服务器下没有被调用。

4
我正在使用FCM服务器进行远程通知,发送和接收通知的功能完美工作。
我的问题是,当设备处于后台时,这个函数 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 没有被调用。
我尝试过的解决方案:
  • This function:

    userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
    

只有当用户点击通知时,该函数才会被调用。否则,它不会被触发。

  • 在载荷中添加"content-available": true, "priority": "high"。此外,我还尝试了这个值 "content-available": 1

  • 以下是我的函数代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
        print("userInfo: \(userInfo)")
      completionHandler(.newData);
  
    }


如果你正在使用 Postman 进行测试,请尝试将 "content-available": true 更改为 "content_available" : truecontent-available 会发送通知,但不会调用 didReceiveRemoteNotification - iUser
是的!这个解决方案解决了我的问题!谢谢你,你真的帮了我! - Noura Aziz
很高兴能够帮到你。您介意将其接受为答案,以便它也可以帮助其他人吗? - iUser
1
当然!你值得拥有那个。 - Noura Aziz
6个回答

3

只是补充接受的答案:在我的情况下,问题是相同的,但由于我通过http "POST" NSMUtableRequest发送通知,因此应用接受的答案的方法实际上是将其与声音、徽章、图块等通知参数一起添加。

let postParams: [String : Any] = [
                "to": receiverToken,
                "notification": [
                    "badge" : 1,
                    "body": body,
                    "title": title,
                    "subtitle": subtitle,
                    "sound" : true, // or specify audio name to play
                    "content_available": true, // this will call didReceiveRemoteNotification in receiving app, else won't work
                    "priority": "high"
                ],
                "data" : [
                    "data": "ciao",
            ]
                ]

希望这也能帮助到其他人,因为我花了很长时间来弄清楚如何应用这个解决方案。 非常感谢。


2
如果您正在使用 Postman 进行测试,请尝试将 "content-available": true 更改为 "content_available" : truecontent-available 将发送通知,但不会调用 didReceiveRemoteNotification

1

要在应用程序的后台和前台查看远程通知并在应用程序运行时显示警报,您需要使用我在代码中使用的这个appDelegate.swift:

import Firebase
import UserNotifications
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate  {
    var window: UIWindow?
    let gcmMessageIDKey = "gcm_message_id"
    var deviceID : String!


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {



        // [START set_messaging_delegate]
        Messaging.messaging().delegate = self
        // [END set_messaging_delegate]

        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()
        FirebaseApp.configure()
        Fabric.sharedSDK().debug = true
        return true

    }

    func showNotificationAlert(Title : String , Message : String, ButtonTitle : String ,window : UIWindow){
        let alert = UIAlertController(title: Title, message: Message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: ButtonTitle, style: .cancel, handler: nil))
        window.rootViewController?.present(alert, animated: true, completion: nil)

    }

    // [START receive_message] remote notification
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.

        print(userInfo)

    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)

    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }

    // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
    // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
    // the FCM registration token.
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("APNs token retrieved: \(deviceToken)")

        // With swizzling disabled you must set the APNs token here.
        // Messaging.messaging().apnsToken = deviceToken
    }
}

// [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

        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        // Print full message.
        print(userInfo)



        // Change this to your preferred presentation option
        completionHandler([.alert, .badge, .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[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }


        if let aps = userInfo["aps"] as? NSDictionary {
            if let alert = aps["alert"] as? NSDictionary {
                if let title = alert["title"] as? NSString {
                    let body = alert["body"] as? NSString
                    showNotificationAlert(Title: title as String, Message: body! as String, ButtonTitle: "Ok", window: window!)
                }
            } else if let alert = aps["alert"] as? NSString {
                print("mohsen 6 =\(alert)")
            }
        }
        // Print full message.
        print(userInfo)


        completionHandler()
    }
}
// [END ios_10_message_handling]

extension AppDelegate : MessagingDelegate {
    // [START refresh_token]
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        deviceID = fcmToken

        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }
    // [END refresh_token]
    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    }
    // [END ios_10_data_message]


}

0

步骤1 - 添加框架

import UserNotifications

步骤2 - 通过 UNUserNotificationCenterDelegate 继承 AppDelegate

final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {}

步骤 3 - 对于registerPushNotifications做出适当的更改,并在didFinishLaunchingWithOptions中调用它

private func registerPushNotifications() {
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: { granted, error in

            if granted {
                UIApplication.sharedApplication().registerForRemoteNotifications()
            } else {
                    // Unsuccessful...
            }
          })
    } else {
        let userNotificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
        let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()
   }
}

步骤四 - 实现新的处理程序

    // leave this for older versions 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}

// For iOS10 use these methods and  pay attention how we can get userInfo
// Foreground push notifications handler
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    if let userInfo = notification.request.content.userInfo as? [String : AnyObject] {
        // Getting user info
    }
    completionHandler(.Badge)
 }

// Background and closed  push notifications handler
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)  {
    if let userInfo = response.notification.request.content.userInfo as? [String : AnyObject] {
        // Getting user info
    }
    completionHandler()
}

不相干的,我不想让你失去希望。Firebase在v5中存在很多问题。这是我在Firebase 3中使用的方法,当它们搞砸了它时,我不得不转到OneSignal。他们还支持CURL。 - excitedmicrobe
是的,我认为是这样的。因为我有其他的问题,这些问题是由于 Firebase 版本引起的。 - Noura Aziz
如果您改变了主意,这并不是一个明智的选择;我会在这里为任何OneSignal相关问题提供帮助。 - excitedmicrobe

0

你需要检查的第一件事是确保 后台模式 > 远程通知 已经开启。如果没有开启,它将无法在后台模式下被调用。


是的,它已经在项目功能中进行了检查。 - Noura Aziz

0

根据您的尝试,我相信您已经启用了后台权限,content-available是有效负载的一部分,并且您在活动状态下也收到了通知。最后一件事是检查省电模式是否关闭。如果您的手机处于省电模式,则后台刷新和其他进程将受到限制,因此应用程序不会在后台调用didReceivedNotifcation


我刚刚尝试了您的解决方案,但不幸的是它没有在后台调用。 - Noura Aziz
"content-available": 1 应该是负载中 aps 的一部分;请查看苹果文档(https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1)。 - M Abubaker Majeed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接