简体   繁体   English

在iOS设备上未收到Firebase推送通知。 尽管控制台记录了该消息,但已在设备上接收到该消息

[英]Firebase push notification is not received on the iOS device. Although console logs the message, it is received on device

I have created a sample ios application as per the instructions provided in the Firebase console to receive cloud messages on ios devices. 我已经按照Firebase控制台中提供的说明创建了一个示例ios应用程序,以在ios设备上接收云消息。 Notification message is sent from the console and gets printed on the app console as well. 通知消息是从控制台发送的,并且也打印在应用程序控制台上。 But the push notification is not received on the device even after giving necessary permission. 但是,即使在给予必要的许可后,设备上也不会收到推送通知。

I'm using swift 3 and xCode version 10.3. 我正在使用Swift 3和xCode版本10.3。 Registered the app in Firebase console and required FCM token is received through the app and added to send a test message to the device. 在Firebase控制台中注册了该应用程序,并且通过该应用程序接收了所需的FCM令牌,并添加了该FCM令牌以向设备发送测试消息。 Tried sending lot of messages through the console but none of them are shown on the device. 尝试通过控制台发送大量消息,但设备上均未显示任何消息。 It shows as the messages are being sent successfully on the console. 它显示消息正在控制台上成功发送。

Following is my AppDelegate.swift file that I have written to receive the push notification. 以下是我为接收推送通知而编写的AppDelegate.swift文件。

import UIKit
import UserNotifications
import Firebase
import FirebaseMessaging
import FirebaseInstanceID

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let gcmMessageIDKey = "gcm.message_id"

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

        if #available(iOS 10.0, *) {

            FirebaseApp.configure()           

            Messaging.messaging().delegate = self
            Messaging.messaging().shouldEstablishDirectChannel = true

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


        // request permission from user to send notification
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { authorized, error in
            if authorized {
                DispatchQueue.main.async(execute: {
                    application.registerForRemoteNotifications()
                })
            }
            else{
                print("Notification access denied")
            }
        })
        return true
    }


    // [START receive_message]
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        print(userInfo)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }

        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Unable to register for remote notifications: \(error.localizedDescription)")
    }


    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
        print("APNs token retrieved: \(deviceToken)")


    }

}

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo

        print(userInfo)
         print("Message ID: \(userInfo["gcm.message_id"]!)")
        completionHandler(UNNotificationPresentationOptions.alert)

    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo

        print("Do what ever you want")
        // Print full message.
        print("tap on on forground app",userInfo)


        completionHandler()
    }
}

extension AppDelegate : MessagingDelegate {

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        Messaging.messaging().subscribe(toTopic: "/topics/nutriewell_live")
        Messaging.messaging().shouldEstablishDirectChannel = true

    }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    }


}

I expected a push notification on the ios device but I'm not receiving any. 我期望在ios设备上收到推送通知,但没有收到任何通知。 I'm getting the following output which gets printed with the notification message sent from the console. 我得到以下输出,该输出与从控制台发送的通知消息一起打印。

2019-08-15 11:20:32.548066+0530 FirebaseIos[800:207059]  - <AppMeasurement>[I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. 




To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist



2019-08-15 11:20:32.949393+0530 FirebaseIos[800:207068] 6.2.0 - [Firebase/Messaging][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. 


Firebase registration token: fsyB51mCvbg:APA91bFvRFPIxauHfKA-v-K29YTWl_TYFTgCpnvODrFA2rG8qN-F8vcLIWcZ-lOChkJw-pIBVhcxg2epBz7AYVALMNLC4Hs6M4ds_pQXytYymjr15KMqOt08_7PkmYkU1jHy6xcw5hvx
2019-08-15 11:20:34.914040+0530 FirebaseIos[800:207059] 6.2.0 - [Firebase/Messaging][I-FCM002024] Format '/topics/nutriewell_live' is deprecated. Only 'nutriewell_live' should be used in subscribeToTopic.
Notification access denied


2019-08-15 11:20:48.869741+0530 FirebaseIos[800:207068] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C7.1:2][0x101537e10] get output frames failed, state 8196




Received data message: [AnyHashable("notification"): {
    body = "firebase ios";
    e = 1;
    tag = "campaign_collapse_key_3627158360856700759";
    title = "firebase!";
}, AnyHashable("from"): 591363996390, AnyHashable("collapse_key"): com.combank.Firebase.Ios]
%@ [AnyHashable("notification"): {
    body = "firebase ios";
    e = 1;
    tag = "campaign_collapse_key_3627158360856700759";
    title = "firebase!";
}, AnyHashable("from"): 591363996390, AnyHashable("collapse_key"): com.combank.Firebase.Ios]





    Received data message: [AnyHashable("collapse_key"): com.combank.Firebase.Ios, AnyHashable("from"): 591363996390, AnyHashable("notification"): {
        body = "firebase ios";
        e = 1;
        tag = "campaign_collapse_key_750970355066402639";
        title = "firebase!";
    }]




%@ [AnyHashable("collapse_key"): com.combank.Firebase.Ios, AnyHashable("from"): 591363996390, AnyHashable("notification"): {
    body = "firebase ios";
    e = 1;
    tag = "campaign_collapse_key_750970355066402639";
    title = "firebase!";
}]




2019-08-15 11:22:34.475335+0530 FirebaseIos[800:207131] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x1015273f0] get output frames failed, state 8196

Any help I can get here for this? 我可以在这里获得任何帮助吗?

There are two ways to receive notification: 有两种接收通知的方式:

  1. when the app is closed/inactive and you get the payload in didFinishLaunchingWithOptions: 当应用程序关闭/处于非活动状态,并且您在didFinishLaunchingWithOptions中获得有效负载时:

  2. when the application is open/active and currently visible you get payload in didReceiveRemoteNotification: 当应用程序处于打开/活动状态并且当前可见时,您会在didReceiveRemoteNotification中获得有效负载:

When your application is closed, iOS take care of displaying a notification (if notification extension not developed) and launch your app when you tap on it. 当您的应用程序关闭时,iOS会显示通知(如果未开发通知扩展名)并在您点击时启动您的应用程序。

If your application is open you have to take care of displaying this notification or ignore it. 如果您的应用程序处于打开状态,则必须注意显示或忽略此通知。

Here what official documentation says: 官方文件说的是:

When local and remote notifications are not handled directly by your app or the user, they are displayed in Notification Center so that they can be viewed later. 当您的应用或用户未直接处理本地和远程通知时,它们会显示在“ 通知中心”中,以便以后查看。 Use the getDeliveredNotificationsWithCompletionHandler: method of the shared UNUserNotificationCenter object to get a list of notifications still being displayed in Notification Center. 使用共享的UNUserNotificationCenter对象的getDeliveredNotificationsWithCompletionHandler:方法来获取仍在通知中心显示的通知列表。 If you find any notifications that are now outdated and should not be displayed to the user, you can remove them using the removeDeliveredNotificationsWithIdentifiers: method. 如果发现现在已经过时且不应显示给用户的任何通知,则可以使用removeDeliveredNotificationsWithIdentifiers:方法将其删除。

official documentation 官方文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM