Firebase Notification To Device with FCM Token Says Sent but not received

后端 未结 4 780
星月不相逢
星月不相逢 2020-12-31 15:54

I am attempting to send a simple push notification from the firebase notification console to a specific device using an FCM token. The firebase notification console shows t

相关标签:
4条回答
  • 2020-12-31 16:25

    A couple of troubleshooting steps I use when working with push notifications are:

    1. Get push working independent of the firebase services first. I use this tool.
    2. Make sure that you dont have any bundle identifier namespace collisions. So for example having any combination of appstore build, testflight build, and / or develop build of an app on the device. Delete all but one instance of the app. The bundle identifier is how your device knows which app to route the push notification to.
    3. When all else fails - I try to isolate the issue by building a new sample project and hook it up to a new firebase project and see if I can narrow my focus down to just being able to get push working without any other business logic in my app. This helps me prove to my self that I haven't gone insane, and proves to me that it's not some mysterious network condition leading to my woes.

    I hope this helps you as you work get it all figured out.

    0 讨论(0)
  • 2020-12-31 16:28

    Try to add the following code to didRegisterForRemoteNotificationsWithDeviceToken func:

    Messaging.messaging().apnsToken = deviceToken
    

    So it will look like this:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
        Messaging.messaging().apnsToken = deviceToken
    }
    

    It is work for me.

    0 讨论(0)
  • 2020-12-31 16:39

    I see that you have done everything in your project's Capabilities.

    Some points:

    • Conform your class to messaging delegate like so:

      Messaging.messaging().delegate = self
      
    • Make sure you've setup your certificates properly and uploaded everything on Firebase Push Notification Configurations (not exact term).

    • I've done several applications that uses Firebase Push Notification Service and making a sample app from scratch might help you figure out what you've been doing wrong.

    And... here's a nicer code block for registering your application for push notification.

        // Setup Push Notifications
    
        if #available(iOS 10.0, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                if error == nil{
                    DispatchQueue.main.async(execute: {
                        application.registerForRemoteNotifications()
                    })
                }
            }
        }
        else {
            let notificationTypes: UIUserNotificationType = [.sound, .alert, .badge]
            let notificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
            application.registerForRemoteNotifications()
            application.registerUserNotificationSettings(notificationSettings)
        }
    
    0 讨论(0)
  • 2020-12-31 16:40

    Does anyone know why the notification is being marked as sent in the firebase notification console but not showing up on the device?

    Because "sent" does not mean "received".

    Notifications cannot be guaranteed to be received on the device. With basic APNS infrastructure you even cannot get the information if a notifications was received or processed on the device.

    If you don't receive a successfully sent message on the device there can be many reasons. Furthermore, even if you receive a Firebase token, that does not mean that your device can receive the notification in any case.

    To isolate the problem I would suggest to build up the minimal setup and use APNS without Firebase. You could use Terminal or NWPusher (https://github.com/noodlewerk/NWPusher) for sending notifications from your local macOS system and the iOS native remote push notifications framework for receiving notifications.

    Keep care to convert the APNS device token to the correct format required for submitting a notification:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
        let token = deviceToken.hexEncodedString()
        print("Token: \(token)")
    }
    

    Data extension:

    extension Data {
        func hexEncodedString() -> String {
            return map { String(format: "%02hhx", $0) }.joined()
        }
    }
    
    0 讨论(0)
提交回复
热议问题