Firebase通知到具有FCM令牌的设备说已发送但未收到 [英] Firebase Notification To Device with FCM Token Says Sent but not received

查看:705
本文介绍了Firebase通知到具有FCM令牌的设备说已发送但未收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用FCM令牌从Firebase通知控制台向特定设备发送简单的推送通知。 Firebase通知控制台显示通知已发送,但设备未收到通知。我已经尝试发送通知,然后等待查看控制台是否从 didReceiveRemoteNotification 进行日志记录,但通知过长(小时)显示为在Firebase控制台中发送即使我将优先级设置为 high )。

应用程序委托

 导入UIKit 
导入Firebase
导入FirebaseStorage
导入FirebaseDatabase
导入FirebaseMessaging
导入CoreData
导入UserNotifications

@UIApplicationMain $ b $ class AppDelegate:UIResponder,UIApplicationDelegate,UNUserNotificationCenterDelegate {

var window:UIWindow?

func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {
//在应用程序启动后替代自定义点。

//使用Firebase库配置API
FirebaseApp.configure()
$ b $ // ///
//对于Firebase云消息传递(FCM )
if #available(iOS 10.0,*){
//对于iOS 10显示通知(通过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()
// [Firebase Cloud Messaging(FCM)]的结尾
/////

返回true
}

///////////////////////
// FCM设置

//监控FCM的令牌生成:每当FCM令牌更新时通知
func消息(_ messaging:Messaging,didRefreshRegistrationToken fcmToken:String){
print(Firebase注册令牌: \\(fcmToken))
}

//为FCM监控令牌生成:
func应用程序(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
Messaging .messaging()。apnsToken = deviceToken
} //处理通过FCM APNs接口接收的消息
func应用程序(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any]){
print (didReceiveRemoteNotification)
//如果您在应用程序处于后台时收到通知消息,则
//直到用户触发此回调点击启动应用程序的通知。
// TODO:处理通知的数据

//如果停用swizzling,则必须让Messaging知道关于消息的信息,Analytics
// Messaging.messaging()。appDidReceiveMessage( userInfo)

//打印消息ID。
// gcm_message_id
如果让messageID = userInfo [gcmMessageIDKey] {
print(Message ID:\(messageID))
}

^我的猜测是这个问题可能与gcm_message_id/gcmMessageId/gcm有关。 message_id,因为它在以下三种方法中都不相同。

  //打印完整的信息。 
print(userInfo)
}

//处理通过FCM APNs接口接收的消息
func应用程序(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any ],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult) - > Void){
print(didReceiveRemoteNotification(withCompletionHandeler))
//如果在应用程序处于后台时收到通知消息,
//直到用户点击启动应用程序的通知,这个回调才会被触发。
// TODO:处理通知的数据

//如果停用swizzling,则必须让Messaging知道关于消息的信息,Analytics
// Messaging.messaging()。appDidReceiveMessage( userInfo)

//打印消息ID。
if messageID = userInfo [gcmMessageIDKey] {
print(Message ID:\(messageID))
}
pre>

^我的猜测是这个问题可能与gcm_message_id/gcmMessageId/gcm.message_id有关,因为它与

  //打印完整的信息。 
print(userInfo)

completionHandler(UIBackgroundFetchResult.newData)
}

// FCM设置结束
/// ////////////////////
}

视图控制器

  class ViewController:UIViewController {

覆盖func viewDidLoad(){
super.viewDidLoad()

//检索Firebase Cloud Messaging(FCM)的当前注册标记
let token = Messaging.messaging()。 fcmToken
print(FCM token:\(token ??))
}
}

权利&启用推送通知



我已经添加了推送权利并启用推送通知的背景模式,并将GoogleService-Info.plist添加到我的项目中。



发送通知的方法 b

我正在从Firebase通知控制台创建通知(如下所示),因此通知本身的结构应该没有问题。

  • 远程通知w / firebase教程

  • Google Firebase Quickstart


    有谁知道为什么通知是标记为已在Firebase通知控制台中发送,但未在设备上显示?

    解决方案

    在使用推送通知时,我使用的两个故障排除步骤是:


    1. 获取推送独立于Firebase服务。我使用这个工具。

    2. 确保你没有任何捆绑标识符命名空间冲突。因此,例如,具有appstore构建,testflight构建和/或在设备上开发应用程序构建的任何组合。删除所有应用程序的一个实例。软件包标识符是您的设备知道将推送通知路由到哪个应用程序的方式。

    3. 当所有其他项都失败时,我会尝试通过构建一个新的示例项目来隔离这个问题,并将其连接到一个新的Firebase项目,并查看是否可以缩小我的焦点在我的应用程序中没有任何其他业务逻辑推动工作。这有助于我向自己证明自己并没有疯狂,并向我证明,这并不是一个神秘的网络状况,导致我的悲哀。

    我希望这可以帮助你,因为你的工作得到了一切。


    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 the notification as sent but the device does not receive it. I have tried sending the notification and then waiting to see if the console logs from didReceiveRemoteNotification, but the notification takes too long (hours) to be shown as sent in the firebase console (even when I set the priority to high).

    App Delegate

    import UIKit
    import Firebase
    import FirebaseStorage
    import FirebaseDatabase
    import FirebaseMessaging
    import CoreData
    import UserNotifications
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            // Use Firebase library to configure APIs
            FirebaseApp.configure()
    
            /////
            // For Firebase Cloud Messaging (FCM)
            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()
            // End of [for Firebase Cloud Messaging (FCM)]
            /////
    
            return true
        }
    
        ///////////////////////
        // FCM Setup
    
        // Monitor token generation for FCM: Be notified whenever the FCM token is updated
        func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
            print("Firebase registration token: \(fcmToken)")
        }
    
        // Monitor token generation for FCM:
        func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            Messaging.messaging().apnsToken = deviceToken
        }    // Handle messages received through the FCM APNs interface
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
            print("didReceiveRemoteNotification")
            // 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.
            // TODO: Handle data of notification
    
            // With swizzling disabled you must let Messaging know about the message, for Analytics
            // Messaging.messaging().appDidReceiveMessage(userInfo)
    
            // Print message ID.
            // gcm_message_id
            if let messageID = userInfo["gcmMessageIDKey"] {
                print("Message ID: \(messageID)")
            }
    

    ^My guess is that the issue may have to do with the "gcm_message_id"/"gcmMessageId"/"gcm.message_id" as it is different in each of the three approaches I tried below

            // Print full message.
            print(userInfo)
        }
    
        // Handle messages received through the FCM APNs interface
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            print("didReceiveRemoteNotification (withCompletionHandeler)")
            // 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.
            // TODO: Handle data of notification
    
            // 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)")
            }
    

    ^My guess is that the issue may have to do with the "gcm_message_id"/"gcmMessageId"/"gcm.message_id" as it is different in each of the three approaches I tried below

            // Print full message.
            print(userInfo)
    
            completionHandler(UIBackgroundFetchResult.newData)
        }
    
        // End of [FCM Setup]
        ///////////////////////
    }
    

    View Controller

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Retrieve the current registration token for Firebase Cloud Messaging (FCM)
            let token = Messaging.messaging().fcmToken
            print("FCM token: \(token ?? "")")
          }
    }
    

    Entitlements & Enabling Push Notifications

    I have added the push entitlements and enabled background modes for push notifications and added the GoogleService-Info.plist to my project.

    Method for sending Notification

    I am creating notification from the Firebase Notifications console (as shown below) so there should be no issue with the structure of the notification itself.

    I have tried the following approaches to remedy the issue, but all have produced the same result:

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

    解决方案

    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.

    这篇关于Firebase通知到具有FCM令牌的设备说已发送但未收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆