Firebase Messenger发送通知,但iOS设备没有收到任何内容

时间:2018-03-27 20:57:42

标签: ios swift firebase firebase-cloud-messaging

我试图收到从firebase云信使发送到单个设备的通知。我已经安装了firebase,当我运行程序时,控制台打印出我尝试用来向该设备发送消息的fcm令牌(我使用的是真正的iPhone)。消息发送正常但我的iPhone上没有任何内容。我在App Delegate中实现了以下代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)



    if #available(iOS 10.0, *)
    {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert])          { (granted, error) in
            if granted
            {
                //self.registerCategory()
            }
        }
        // For iOS 10 data message (sent via FCM)
        Messaging.messaging().delegate = self
    }
    else
    {
        let settings: UIUserNotificationSettings =  UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    application.registerForRemoteNotifications()
    //Configuring Firebase
    FirebaseApp.configure()
    // Add observer for InstanceID token refresh callback.
    NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)


    return true
}


//Receive Remote Notification on Background
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
    Messaging.messaging().appDidReceiveMessage(userInfo)
}

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

    Messaging.messaging().apnsToken = deviceToken

}

@objc func tokenRefreshNotification(notification: NSNotification)
{
    if let refreshedToken = InstanceID.instanceID().token()
    {
        print("InstanceID token: \(refreshedToken)")
    }
    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}

func connectToFcm()
{
    Messaging.messaging().shouldEstablishDirectChannel = true
}

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

    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
}

This is the response I get from the Firebase end

所以它说没有发送消息,我很困惑,因为它是我的设备刚刚输出的令牌。

如何了解或解决此问题?

1 个答案:

答案 0 :(得分:0)

从fcm控制台运行messege之前检查.p12文件已上传

enter image description here

然后实现消息处理方法 在前景和背景

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    Messaging.messaging().appDidReceiveMessage(userInfo)
    if let messageID = userInfo[gcmMessageIDKey] {
    }

    print(userInfo)
    completionHandler([.alert, .badge, .sound])

   }

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }


    completionHandler()

}

extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: (fcmToken)")
}
Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: (remoteMessage.appData)")
}
}