chemerisuk / cordova-plugin-firebase-messaging

Cordova plugin for Firebase Cloud Messaging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

onMessage and onBackgroundMessage are never called

Allyda opened this issue · comments

commented

I am trying to implement push notifications in my ionic project.
It looks like my hooks defined for onMessage and onBackgroundMessage are not working and get never called, even when the phone definitely receives the push notification.

I configured Firebase and Xcode like described and I also receive the test Firebase push notification when my app is closed on my phone. But my registered callback functions are never called.

I receive the push notification also in the native app with the following code:

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

But it is never handed over to ionic. See my code example below:

FirebaseMessaging.requestPermission({forceShow: false}).then(() => {
        FirebaseMessaging.getToken().then(token => {
            console.log('Ionic Device Token: ' + token);
            alert('Device Token: ' + token);

            FirebaseMessaging.onMessage().subscribe((data) => {
                alert('Received notification');
                console.log('Ionic Push Notification Received', JSON.stringify(data));
            });

            FirebaseMessaging.onBackgroundMessage().pipe(tap(data => {
                alert('Received notification');
                console.log('Ionic Push Notification Received', JSON.stringify(data));
            })).subscribe();

            // alternative try:
            cordova.plugins.firebase.messaging.onMessage(function(payload) {
                alert('Received notification');
                console.log("New foreground FCM message: ", payload);
            });
        });

    });