ios - Firebase 云消息传递未收到通知 (iOS)

标签 ios swift firebase push-notification firebase-cloud-messaging

我正在尝试使用 Firebase Notification Composer 向我的设备发送测试通知。我正在接收 FCM token 并将其打印到我的控制台,然后我尝试向该 token 发送通知。

这是我检查过的内容:

1) 我在 iOS 12.4.1

2) 我正在分配消息传递委托(delegate)

3) 我从委托(delegate)方法接收 FCM token

4)我已经通过打印到控制台验证了通知已启用,当我被提示允许通知时,我单击了允许

5) 我已经验证在 Firebase 项目设置中有一个 APNs Auth Key 上传,具有正确的 TeamID 和 KeyID

6) 发送测试消息时没有调用委托(delegate)方法 willPresentNotifications

7)我尝试过启用/禁用 Swizzling,但都不起作用

8)调试没有错误

这是代码:

import UIKit
import Firebase
import UserNotifications


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?

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

    UITabBar.appearance().barTintColor = UIColor(named: "Splish")!
    UINavigationBar.appearance().barTintColor = UIColor(named: "Splish")!
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

    FirebaseApp.configure()

    Messaging.messaging().delegate = self

    UNUserNotificationCenter.current()
        .requestAuthorization(options: [.alert, .sound, .badge]) {granted, error in

            print("Permission granted: \(granted)")
    }

    UNUserNotificationCenter.current().delegate = self

    application.registerForRemoteNotifications()

    return true
}


func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

    print("FCM Token Is: \(fcmToken)")
}

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

    print("Token is: \(deviceToken)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

    print("Error is \(error)")
}

最佳答案

对于使用 Swift 的 iOS 14.4,我使用了 Firebase 的示例 here .
在@C6Silver 的帮助下建议在功能中启用“推送通知”,然后我能够接收 fcm 测试通知。
祝大家编码愉快!

关于ios - Firebase 云消息传递未收到通知 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61032022/

相关文章:

ios - 如何在 Core Animation 中平滑地更新动画的 toValue/end 点?

ios - 在 UIPickerView Swift 中获取工作工具栏

objective-c - 如何使用 Objective-c 的闭包调用 Swift 方法?

ios - 带有外部源的贴纸扩展

android - 无法在 iOS [Firebase] 中接收通知

ios - 点击更新 tableView 单元格中的 Firebase 数据

ios - 在 uiviewcontroller 中添加 subview 使其正常运行

ios - 我该怎么做 : library's callback wait until user choose from popover and then get return

firebase - 实时数据库 Firebase "All or nothing"事务

ios - 以编程方式添加带有自动布局的 UITableView