使用 FCM 推送通知无法在 iOS 上工作

问题描述 投票:0回答:3

我已经实施了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 云消息传递选项发送通知,但没有发送到任何推送设备。 我也尝试过邮递员。这是我的有效负载和输出

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

我尝试进入主项目,但它不起作用。 然后我尝试将新的实现放入一个虚拟项目中,但它也不起作用。

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

ios swift swiftui firebase-cloud-messaging apple-push-notifications
3个回答
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"
                                }
                        }
               }
 }

0
投票

我建议在 Xcode 中运行 Flutter 项目的 iOS 端。 Xcode对错误的反馈非常像AI,都是Apple官方信息,可以更快取得进展。如果不是在 Xcode(版本 15.2)中运行我的代码,我就会被困在这个问题所在的地方。

2024-01-26 15:47:51.652013+0000 Runner[669:15902] 10.18.0 - [FirebaseCore][I-COR000008] The project's Bundle ID is inconsistent with either the Bundle ID in 'GoogleService-Info.plist', or the Bundle ID in the options if you are using a customized options. To ensure that everything can be configured correctly, you may need to make the Bundle IDs consistent. To continue with this plist file, you may change your app's bundle identifier to 'com.example.myApp'. Or you can download a new configuration file that matches your bundle identifier from https://console.firebase.google.com/ and replace the current one.
2024-01-26 15:47:51.761606+0000 Runner[669:15902] 10.18.0 - [FirebaseMessaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
2024-01-26 15:11:55.643576+0000 Runner[423:8565] 10.18.0 - [FirebaseCore][I-COR000003] The default Firebase app has not yet been configured. Add `FirebaseApp.configure()` to your application initialization. This can be done in in the App Delegate's application(_:didFinishLaunchingWithOptions:)` (or the `@main` struct's initializer in SwiftUI).


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.