简体   繁体   中英

Push Notification Not Working iOS Using FCM

I have implemented FCM. There is no error showing. Token successfully generated. But no notification coming into the device.

I followed the google documentations.

Here is my AppDelegate class

import Foundation
import UIKit
import Firebase

//https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {


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

    FirebaseApp.configure()
    Messaging.messaging().delegate = self

              // 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 })
    
    return true
}


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

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  // Print full message.
  print(userInfo)
  completionHandler(UIBackgroundFetchResult.newData)
}


func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("Unable to register for remote notifications: \(error.localizedDescription)")
}
    
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  print("Firebase registration token: \(String(describing: fcmToken))")
  
  let dataDict:[String: String] = ["token": fcmToken ?? ""]
  NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)

}


}

didReceiveRegistrationToken delegate method callings twice

Here is my Capabilities在此处输入图像描述

I tried send notification from firebase cloud messaging option, but not coming to any Push in device. I also tried from postman. Here is my payload and output

在此处输入图像描述

I have uploaded the APNS certificate in firebabse. 在此处输入图像描述

I tried into main project and its not working. Then I tried to fresh implement into a dummy project its also not working.

Here is my source code. You may check into this. https://www.dropbox.com/s/22wojqv5u3vwjzt/Swift%20UI%20Push%20Notification.zip?dl=0

I think the payload is different for APNS.From the document I found that the following is the format for FCM payload in APNS

 {
   "message":  {
                "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
                "notification" : {
                                  "body" : "This is an FCM notification that displays an image.!",
                                  "title" : "FCM Notification",
                                  },
                "apns": {
                       "payload": {
                                  "aps": {
                                         "mutable-content": 1
                                         }
                                  },
                "fcm_options": {
                                "image": "url-to-image"
                                }
                        }
               }
 }

You can add all the UNUserNotificationCenterDelegate method along with completionHandler([.alert, .badge, .sound]).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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