2

I want to implement push notification using Firebase Cloud Messaging

I have setup my project and uploaded APN certificate as explained and I am sending Test messages using fcmtoken to my real device

my configuration is as follows in AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        registerForPushNotifications(app: application)
        return true
    }

    func registerForPushNotifications(app: UIApplication) {
        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (authorized, error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            if authorized {
                print("authorized")
                DispatchQueue.main.async {
                  UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                print("denied")
            }
        }
        app.registerForRemoteNotifications()
    }

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

    func application(_ application: UIApplication,
        didReceiveRemoteNotification notification: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("notofication arrivied")
      if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
      }
      // This notification is not auth related, developer should handle it.
    }

it is supposed to see notofication arrivied but it didn't Also set a beak point It seems this part is never being excused thus message is not coming

  • @Eilon from FCM console using fcmToken of my device I send a test message using this https://firebase.google.com/docs/cloud-messaging/ios/first-message#send_a_notification_message –  Feb 06 '20 at 13:37
  • 1
    Is it only "didReceiveRemoteNotification" that is not called or you don't get notifications at all? – Konstantin Oznobihin Feb 06 '20 at 13:49
  • @KonstantinOznobihin yes it is only the one not called –  Feb 06 '20 at 14:09
  • 1
    Did you add your APNs Authentication Key or certificates in Firebase Console/Project settings/Cloud Messaging? – Dan Abnormal Feb 06 '20 at 15:08
  • @KonstantinOznobihin yes I checked it didn't solve my problem –  Feb 06 '20 at 17:26
  • @DanAbnormal Yes I have added it –  Feb 06 '20 at 17:26

2 Answers2

2

I don’t see this in your AppDelegate unless you have Swizzling enabled

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
      Messaging.messaging().apnsToken = deviceToken
    }

This code maps your APNs device token to FCM token, which is necessary because APNs token is the only way you can send a push notification.

1
func sendPushNotification(to token: String, title: String, body: String, userInfo: [String: Any]) {
    let payload: [String: Any] = ["title": title, "body": body, "sound": "sound.caf"]
    let paramString: [String: Any] = ["to": token, "notification": payload, "data": userInfo]
    
    let urlString = "https://fcm.googleapis.com/fcm/send"
    let url = NSURL(string: urlString)!
    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"
    request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("key=\(Keys.gmsServerKey)", forHTTPHeaderField: "Authorization")

    let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
        do {
            if let data = data {
                if let object  = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any] {
                    NSLog("Received data: \(object))")
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
    }
    task.resume()
}
rakita
  • 39
  • 7
  • It's always recommended to provide an explanation for the given code, more details you write, more the answer will be useful – xKobalt Aug 19 '20 at 15:22