FCM iOS推送通知无法接收任何通知



我目前正在处理通知,但尚未成功从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

我已经从Background fetches激活了Push NotificationsRemote Notifications,并且已经阅读了stackoverflow中当前类似的另一个主题,在这里是

我想和大家分享我的AppDelegate代码,但首先我必须说,谷歌推送通知的文档似乎有点令人困惑,因为这里有太多被高估的方法,每个教程都有不同的方式来接收通知。

我已经进口了这些

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的委托函数。

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令牌。

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. 创建开发APN证书并将其添加到Firebase控制台项目设置

  3. 在您的应用程序代理

    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")
    }
    
    }
    

我使用的是FirebaseMessaging v4.4.4版,并已启用方法swizzling

application.registerForRemoteNotifications((-->第一个

Messaging.Messaging((.delete=self-->第二个

您必须在请求远程通知后设置消息代理。(我注意到你在请求许可之前打电话给它(

在我的情况下,因为我之前调用了它,所以我在iOS 13的didRegisterForRemoteNotificationsWithDeviceToken方法中没有收到APNS令牌,而firebase令牌是生成的。

也许是因为没有生成APNS,也没有使用APNS令牌映射firebase,所以您可能没有收到任何通知。

他们说注意:要以这种方式使用FCM直接通道,必须使用传统的HTTP API发送消息。HTTP v1 API对发送到iOS设备的所有消息使用APN。你需要把Messaging.Messaging((.huldEstablishedDirectChannek=真

我在ios 13设备上遇到了同样的问题。但它在低于ios 13的设备上运行良好。问题是没有在ios 13设备上调用didRegisterForRemoteNotificationsWithDeviceToken方法。经过一些研究,我发现这是由于网络连接问题。然后我从办公室的无线网络切换到蜂窝网络,然后它就工作了。didRegisterForRemoteNotificationsWithDeviceToken返回了设备令牌。

我在这里回答了一个类似的问题。

关键的一步是使";推送通知";在能力方面。

祝大家编码愉快!

相关内容

  • 没有找到相关文章