Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM iOS Push Notification cannot receive any notification

I've currently working with notifications and I've not succeed to get any notification from the Firebase FCM.

The podfile configuration is:

target 'Project' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Project
  pod 'Firebase/Core'
  pod 'Firebase/Messaging'
end

I've activated Push Notifications and Remote Notifications from the Background fetches and already read the another topic in stackoverflow which is currently similar, in here

I want to share my AppDelegate codes with you but firstly I have to say the documents of the Google for push notifications seems a bit confusing because there are so many overrided method in here and every tutorial has different way to accomplish to receive a notification.

I've imported these

import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging

Then there is the delegations

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}

Then here is the willFinishLaunchWithOptions method

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self



        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 })
            // For iOS 10 data message (sent via FCM
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)

        }

        application.registerForRemoteNotifications()


        return true
    }

And here is the Messaging delegate functions.

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("MEssage ->  \(remoteMessage.appData)")
    }

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

That function was in the Firebase documentation for setting apns token.

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

I've sent hundreds of notifications from the FCM' and it sent successfully on the server side but when i log the received message there is nothing to income data.

If you ask that why I implement configuration in willFinish function there was a note in the documentation like that.

        For devices running iOS 10 and above, you must assign the
     UNUserNotificationCenter's delegate property and FIRMessaging's 
delegate property. For example, in an iOS app, assign it in the 
applicationWillFinishLaunchingWithOptions: or 
applicationDidFinishLaunchingWithOptions: method of the app delegate.

I would appreciate that any help from you, because its hard to see what's wrong with it for now.

like image 411
eemrah Avatar asked Dec 14 '18 19:12

eemrah


People also ask

Does FCM work on iOS?

Stay organized with collections Save and categorize content based on your preferences. For Apple client apps, you can receive notification and data payloads up to 4000 bytes over the Firebase Cloud Messaging APNs interface.

Is there any limit for Firebase push notification?

Notification messages can contain an optional data payload. Maximum payload for both message types is 4000 bytes, except when sending messages from the Firebase console, which enforces a 1024 character limit.

What is iOS APN?

Apple Push Notification service (APNs) is a cloud service that allows approved third-party apps installed on Apple devices to send push notifications from a remote server to users over a secure connection. For example, a newstand app might use APNs to send a text alert to an iPhone user about a breaking news story.


1 Answers

I'm using FirebaseMessaging v4.1.4 and have enabled method swizzling

application.registerForRemoteNotifications() --> First

Messaging.messaging().delegate = self --> Second

You have to set messaging delegate after requesting for remote notification. (I noticed that you're calling it before requesting for permission)

In my case because I called it before I did not receive APNS Token in didRegisterForRemoteNotificationsWithDeviceToken method for iOS 13 whereas firebase token was generated.

Maybe because the APNS was not generated and firebase was not mapped with the APNS Token you might have not received any notification.

like image 59
kadar Avatar answered Nov 15 '22 16:11

kadar