Using onNotificationOpen() in cordova-plugin-firebase

Hi,
I have been trying to implement FCM in my Ionic 2 app. I have added the firebase plugin from ionic native, and the integration appears to be working nicely on my android phone. I can receive the data of the push-notifications when my app is in background mode, since the display of notfications is handled automatically in that case. However, when the app is in foreground mode I am not sure how to actually retrieve the notification data in the callback. I receive an object from firebase with a tap property that is true if the app is in background mode and false if in foreground mode. It is not clear to me from expecting the object in the console where to retrieve the data from, or if it is even there. I have tried doing res.data and JSON.stringify on res (only contains the tap property), but to no avail. Here’s my code:

import { Firebase } from 'ionic-native';

Firebase.getToken()
  .then(token => console.log(`The token is ${token}`)) // save the token server-side and use it to push notifications to this device
  .catch(error => console.error('Error getting token', error));

Firebase.onTokenRefresh()
  .subscribe((token: string) => console.log(`Got a new token ${token}`));

Firebase.onNotificationOpen()
  .subscribe(res => {
    if(res.tap) {
      // background mode
      console.log("background");
      console.log(res);
    } else if (!res.tap) {
      // foreground mode
      console.log("foreground");
      console.log(res);
    }
  });

Any clues on how to get the data in the onNotificationOpen callback? Or should be fetched in another way? Since the ionic2 implementation is using observables I cannot turn to the actual github documentation of the project.

1 Like

I haven’t been able to get that to work other than to console.log() the data inside the notification.

One of my readers told me that it was not possible and that he had to use the push notification plugin for FCM (the one that doesn’t have Ionic native wrapper) and that way he got it to work.

I’m trying to get it to work using the Firebase plugin since it would make things a lot easier, if I have any success with it I’ll let you know

1 Like

Hi javebratt,

Any success of this? Please share it with us.I have successfully implemented analytics part using this plugin.But it would be great if you can provide more about push notification with this native plugin.Thanks.

It’s a bug in the Cordova plugin, it doesn’t work when the app is killed.

You will need the FCM Cordova Plugin

2 Likes

Hi Mejiora,

You have a solution below.There is a git repo link too.It’s working perfectly for both Foreground and background use cases.

Stackoverflow


update your firebase plugin code for android. it would help you.