Search code examples
androidiosreactjscordovafirebase-cloud-messaging

Firebase push notifications with cordova foreground and background n


I am working on Cordova with react Project. I have set up google firebase google-services.json and GoogleService-Info.plist for android and ios. I am using the below package for firebase notifications setup.

https://github.com/andrehtissot/cordova-plugin-fcm-with-dependecy-updated

FCM.requestPushPermission({
    ios9Support: {
        timeout: 10, // How long it will wait for a decision from the user before returning `false`
        interval: 0.3, // How long between each permission verification
    },
}).then((wasPermissionGiven) => {
    console.log(wasPermissionGiven);
    alert(JSON.stringify(wasPermissionGiven));
    if (wasPermissionGiven) {
        FCM.getAPNSToken().then((apnsToken) => {
            alert(JSON.stringify(apnsToken));
            console.log(apnsToken);
        });

        FCM.getToken().then((apnsToken) => {
            alert(JSON.stringify(apnsToken));
            console.log(apnsToken);
        });

        FCM.subscribeToTopic('handsome');
        const disposables = FCM.onNotification((payload) => {
            alert(JSON.stringify(payload.toString()));
            console.log('payload', payload);
        });
    }
});

By using the above code I am getting token and payload. Currently, I am populating the payload with a popup. can someone help me with implementing a notification instead of a popup? I searched a lot in google not found any references to implement notification with Cordova react.


Solution

  • Case 1: Testing app in Background State

    Send the notification from firebase cloud messaging and test the APP in background state / minimise the app then you'll see the notification.

    Case 2: Testing app in Foreground State

    You should use some plugin something similar to this plugin https://github.com/katzer/cordova-plugin-local-notifications#readme to get the firebase notifications While app is in foreground.