【问题标题】:Push Notification Not Working iOS Using FCM使用 FCM 推送通知不工作 iOS
【发布时间】:2021-02-11 10:07:23
【问题描述】:

我已经实施了 FCM。没有错误显示。令牌成功生成。但没有通知进入设备。

我遵循了谷歌文档。

这是我的 AppDelegate 类

import Foundation
import UIKit
import Firebase

//https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    FirebaseApp.configure()
    Messaging.messaging().delegate = self

              // 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 })
    
    return true
}


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  print(userInfo)
}

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  // Print full message.
  print(userInfo)
  completionHandler(UIBackgroundFetchResult.newData)
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("Unable to register for remote notifications: \(error.localizedDescription)")
}
    
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  print("Firebase registration token: \(String(describing: fcmToken))")
  
  let dataDict:[String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

}


}

didReceiveRegistrationToken 委托方法调用两次

这是我的能力

我尝试从 firebase 云消息传递选项发送通知,但没有发送到任何 Push in 设备。 我也试过邮递员。这是我的有效载荷和输出

我已经在 firebabse 上传了 APNS 证书。

我尝试了主项目,但它不工作。 然后我尝试重新实施到一个虚拟项目中,它也不起作用。

这是我的源代码。你可以检查一下这个。 https://www.dropbox.com/s/22wojqv5u3vwjzt/Swift%20UI%20Push%20Notification.zip?dl=0

【问题讨论】:

  • 检查有效负载,但在文档中 APNS 的 fcm 有效负载不同
  • 在 iOS 上使用 FCM 总是很困难,但在 Android 上总是运行得更好。曾经与 Firebase 团队就他们的 iOS 不稳定 FCM 进行了长时间的讨论。以后请使用 APNs,它总是很稳定的。

标签: ios swift swiftui firebase-cloud-messaging apple-push-notifications


【解决方案1】:

我认为APNS的payload是不同的。从文档中我发现以下是APNS中FCM payload的格式

 {
   "message":  {
                "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
                "notification" : {
                                  "body" : "This is an FCM notification that displays an image.!",
                                  "title" : "FCM Notification",
                                  },
                "apns": {
                       "payload": {
                                  "aps": {
                                         "mutable-content": 1
                                         }
                                  },
                "fcm_options": {
                                "image": "url-to-image"
                                }
                        }
               }
 }

【讨论】:

  • 这不是邮递员的工作。但是,我的有效负载在我的其他应用程序中工作。
【解决方案2】:

您可以将所有 UNUserNotificationCenterDelegate 方法与 completionHandler([.alert, .badge, .sound]) 一起添加。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2018-03-05
    • 2018-04-24
    相关资源
    最近更新 更多