【问题标题】:Firebase messenger sends a notification but iOS device doesn't receive anythingFirebase Messenger 发送通知,但 iOS 设备未收到任何信息
【发布时间】:2018-09-06 10:00:56
【问题描述】:

我正在尝试获取从 firebase 云信使发送到单个设备的通知。我已经安装了 firebase,当我运行程序时,控制台会打印出我尝试用来向该设备发送消息的 fcm 令牌(我使用的是真正的 iPhone)。消息发送正常,但我的 iPhone 上没有出现任何内容。我在 App Delegate 中实现了以下代码:

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



    if #available(iOS 10.0, *)
    {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert])          { (granted, error) in
            if granted
            {
                //self.registerCategory()
            }
        }
        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self
    }
    else
    {
        let settings: UIUserNotificationSettings =  UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
    //Configuring Firebase
    FirebaseApp.configure()
    // Add observer for InstanceID token refresh callback.
    NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)


    return true
}


//Receive Remote Notification on Background
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
    Messaging.messaging().appDidReceiveMessage(userInfo)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{

    Messaging.messaging().apnsToken = deviceToken

}

@objc func tokenRefreshNotification(notification: NSNotification)
{
    if let refreshedToken = InstanceID.instanceID().token()
    {
        print("InstanceID token: \(refreshedToken)")
    }
    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}

func connectToFcm()
{
    Messaging.messaging().shouldEstablishDirectChannel = true
}

func applicationDidBecomeActive(application: UIApplication)
{
    connectToFcm()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")

    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
}

This is the response I get from the Firebase end

所以它说没有发送任何消息,我对此感到困惑,因为这是我的设备刚刚输出的令牌。

如何了解或解决此问题?

【问题讨论】:

  • 您是否上传了用于发送通知的授权密钥?请点击以下链接:firebase.google.com/docs/cloud-messaging/ios/certs
  • 1.仔细检查@vishalwaka 发布的页面中的信息(即捆绑 ID 等) 2. 如果您使用的是证书(而不是密钥),请确保您使用的是开发密钥而不是生产密钥
  • 您是否在您的应用程序info.plist 文件中设置了属性FirebaseAppDelegateProxyEnabled。如果你做了,你给了它什么价值?
  • @Barns 我在 Info.plist 中将 FirebaseAppDelegateProxyEnabled 设置为 NO
  • @PeterTao 我正在使用 APN 密钥或 .p8 文件

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

在从 fcm 控制台运行消息之前检查 .p12 文件是否已上传

然后实现消息处理方法 在前景和背景中

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    if let messageID = userInfo[gcmMessageIDKey] {
    }

    print(userInfo)
    completionHandler([.alert, .badge, .sound])

   }

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }


    completionHandler()

}

extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: (fcmToken)")
}
Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: (remoteMessage.appData)")
}
}

【讨论】:

  • 我没有上传APN文件,但现在我上传了,我也实现了你的代码,仍然没有任何变化。它发送但未收到。
  • 我使用的是 .p8 文件,因为我听说它们很好,但我现在决定尝试使用 .p12 文件,看看是否是问题所在。我遇到了 bundleID 的问题,也许你会在这里看到我的问题:stackoverflow.com/questions/49537059/…
  • 您上传的文件与您的 buble id 不匹配
  • 放开链接生成.p12文件appcoda.com/push-notification-ios
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2019-04-12
  • 2017-10-04
  • 1970-01-01
  • 2016-02-11
  • 2016-09-17
  • 2017-08-28
相关资源
最近更新 更多