gpt4 book ai didi

swift - FCM iOS 推送通知收不到任何通知

转载 作者:可可西里 更新时间:2023-11-01 00:54:20 25 4
gpt4 key购买 nike

我目前正在处理通知,但我没有成功从 Firebase FCM 获得任何通知。

podfile 配置为:

target 'Project' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for Project
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end

我已经激活了 Push NotificationsRemote Notifications来自 Background fetches并已阅读 stackoverflow 中的另一个主题目前相似,在here

我想与您分享我的 AppDelegate 代码,但首先我不得不说 Google 的推送通知文档看起来有点困惑,因为这里有太多重写的方法,每个教程都有不同的方法来完成接收通知通知。

我已经导入了这些

import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging

然后是代表团

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}

然后这里是willFinishLaunchWithOptions方法

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self



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 })
// For iOS 10 data message (sent via FCM
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)

}

application.registerForRemoteNotifications()


return true
}

这里是 Messaging委托(delegate)功能。

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("MEssage -> \(remoteMessage.appData)")
}

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

该函数在 Firebase 文档中用于设置 apns token 。

func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}

我已经从 FCM 发送了数百条通知,并且在服务器端成功发送,但是当我记录收到的消息时,没有任何收入数据。

如果你问我为什么要在willFinish中实现配置功能文档中有这样的注释。

        For devices running iOS 10 and above, you must assign the
UNUserNotificationCenter's delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.

如果您能提供任何帮助,我将不胜感激,因为目前很难看出它有什么问题。

最佳答案

  1. 请检查您是否在项目功能中激活了推送通知

  2. 创建开发 APNs 证书并将其添加到 Firebase 控制台项目设置

  3. 在您的 App Delegate 中

    import FirebaseMessaging
    import UserNotifications



    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate,
    UNUserNotificationCenterDelegate, MessagingDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.



    //REMOTE NOTIFICATION

    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()

    Messaging.messaging().delegate = self

    let token = Messaging.messaging().fcmToken
    print("FCM token: \(token ?? "")")


    //Added Code to display notification when app is in Foreground
    if #available(iOS 10.0, *) {
    UNUserNotificationCenter.current().delegate = self
    } else {
    // Fallback on earlier versions
    }


    return true
    }


    func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken as Data
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    // Print full message.
    print(userInfo)

    }

    // This method will be called when app received push notifications in foreground
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    { completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
    }


    // MARK:- Messaging Delegates
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    InstanceID.instanceID().instanceID { (result, error) in
    if let error = error {
    print("Error fetching remote instange ID: \(error)")
    } else if let result = result {
    print("Remote instance ID token: \(result.token)")
    }
    }
    }


    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("received remote notification")
    }



    }

关于swift - FCM iOS 推送通知收不到任何通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53785509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com