简体   繁体   English

无法将通知发送到iOS(iPhone)Firebase云消息传递

[英]Can Not Send Notification to iOS (iPhone) Firebase Cloud Messaging

In Info.plist , I have added FirebaseAppDelegateProxyEnabled to NO (boolean) , and capabilities for Background Modes --> Remote Notification is ON and Push Notification is ON too. Info.plist ,我将FirebaseAppDelegateProxyEnabled添加到NO (boolean) ,并且Background Modes --> Remote Notification功能Background Modes --> Remote Notification处于启用状态,而Push Notification也处于启用状态。

And on the iPhone device, has running with dialog to "allow notification".. 并在iPhone设备上运行了带有“允许通知”对话框。

I have some codes like: 我有一些代码,例如:

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

    let gcmBroadcast = "gcm.broadcast"

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


        Messaging.messaging().delegate = self

        // Use Firebase library to configure APIs

        let firebasePListFileName = ControllerEnvironment.getGoogleService()

        let firebaseOptions = FirebaseOptions(contentsOfFile: Bundle.main.path(forResource: firebasePListFileName, ofType: "plist")!)

        FirebaseApp.configure(options: firebaseOptions!)



//        firebase cloud messaging

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

        return true

    }



    // [START receive_message]

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

        // If you are receiving a notification message while your app is in the background,

        // this callback will not be fired till the user taps on the notification launching the application.

        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics

         Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print message ID.

        if let messageID = userInfo[gcmBroadcast] {

            print("Message ID: \(messageID)")

        }

        // Print full message.
        print(userInfo)

    }


    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],

                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        // If you are receiving a notification message while your app is in the background,

        // this callback will not be fired till the user taps on the notification launching the application.

        // TODO: Handle data of notification

        // With swizzling disabled you must let Messaging know about the message, for Analytics

         Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print message ID.

        if let messageID = userInfo[gcmBroadcast] {

            print("Message ID: \(messageID)")

        }

        // Print full message.
        print(userInfo)

        completionHandler(UIBackgroundFetchResult.newData)

    }

    // [END receive_message]

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

        print("Unable to register for remote notifications: \(error.localizedDescription)")

    }


    // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.

    // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to

    // the FCM registration token.

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

        print("APNs token retrieved: \(deviceToken)")

        // With swizzling disabled you must set the APNs token here.

         Messaging.messaging().apnsToken = deviceToken

    }

}







// [START ios_10_message_handling]

@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


        // With swizzling disabled you must let Messaging know about the message, for Analytics

        // Messaging.messaging().appDidReceiveMessage(userInfo)

        // Print message ID.

        if let messageID = userInfo[gcmBroadcast] {

            print("Message ID: \(messageID)")

        }


        // Print full message.

        print(userInfo)



        // Change this to your preferred presentation option

        completionHandler([])

    }


    func userNotificationCenter(_ center: UNUserNotificationCenter,

                                didReceive response: UNNotificationResponse,

                                withCompletionHandler completionHandler: @escaping () -> Void) {

        let userInfo = response.notification.request.content.userInfo

        // Print message ID.

        if let messageID = userInfo[gcmBroadcast] {

            print("Message ID: \(messageID)")

        }


        // Print full message.

        print(userInfo)


        completionHandler()

    }

}

// [END ios_10_message_handling]


extension AppDelegate : MessagingDelegate {

    // [START refresh_token]

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

        print("Firebase registration token: \(fcmToken)")


        let dataDict:[String: String] = ["token": fcmToken]

        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

        // TODO: If necessary send token to application server.

        // Note: This callback is fired at each app startup and whenever a new token is generated.

    }

    // [END refresh_token]

    // [START ios_10_data_message]

    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.

    // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {

        print("Received data message: \(remoteMessage.appData)")

    }

    // [END ios_10_data_message]

}

and logs: 和日志:

Firebase registration token: f6MUKT270iw:APA9.......Eg
APNs token retrieved: 32 bytes

I tried sending Push using firebase cloud messaging console as: User segment -> app and got status completed in firebase list of messages. 我尝试使用Firebase云消息传递控制台将Push发送为: User segment -> app并在消息的Firebase列表中完成了状态。

But, actually my iPhone did not receive the notification. 但是,实际上我的iPhone没有收到通知。

How can I solve the issue? 我该如何解决这个问题?

On iOS the app needs to be not in foreground, as otherwise the developer is informed of the push notification and can manually display it / handle it otherwise -> make sure that the app is not in the foreground 在iOS上,该应用程序不必位于前台,否则开发人员会收到推送通知的通知,并可以手动显示/处理该通知->确保该应用程序不在前台

You also need to add an APNs key or APNs certificates to Firebase. 您还需要向Firebase添加APNs密钥或APNs证书。

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

相关问题 在 iOS 上未收到 Firebase 云消息传递通知 - Firebase Cloud Messaging notification not received on iOS Firebase Cloud Messaging iOS:无后台推送通知 - Firebase Cloud Messaging iOS: No background push notification Firebase 云消息未收到通知 (iOS) - Firebase Cloud Messaging Not Receiving Notification (iOS) 无法向 Firebase Cloud Messaging 发送 iOS CriticalAlert - Not able to send an iOS CriticalAlert to Firebase Cloud Messaging 如何使用 Firebase Cloud Messaging 发送静默通知/后台通知? - how to send silent notification / background notification using Firebase Cloud Messaging? 在iOS Firebase Cloud Messaging(FCM)中,将APNS发送到主题,但发送方不是来自iPhone - In iOS Firebase Cloud Messaging (FCM), send APNS to topic but other than sender from iphone Firebase 云 function 向 iPhone 发送通知 - Firebase cloud function send notification to iPhone 我无法向我的应用程序发送 Firebase Cloud Messaging 推送通知我按照步骤操作但无处可去 - I can't send a Firebase Cloud Messaging push notification to my app i followed the steps but got nowhere 如果由我自己的服务器组成,为什么我无法发送推送通知(Firebase Cloud Messaging) - Why I Can't send the push notification (Firebase Cloud Messaging) if composed from my own Server Firebase Cloud Messaging PHP集成发送推送通知 - Firebase Cloud Messaging PHP Integration send push notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM