Sampath
Sampath

Reputation: 65870

Firebase native plugin with push notifications is not working

I have tried to use Firebase native plugin to send push notifications.But it is not working (not getting a message to the real device).Can you tell me how to do that?

app.component.ts

constructor(platform: Platform, private firebase: Firebase) {

    platform.ready().then(() => {
        this.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));

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

      });
}

After the above implementation, I have tried to send push notification using User Segment on firebase compose message console.

Upvotes: 4

Views: 3478

Answers (1)

naomi
naomi

Reputation: 2643

There could be different reasons for push notifications not work. I provided a set of steps to follow in order to implement push notifications. Take a look maybe you have missed something.

Steps to implement push notifications in Ionic app (for android):

  1. Create a new firebase project

Note: The Firebase package name must be identical to the app id in the config.xml.

  1. Download the google-services.json file and put it in the root directory of your app.
  2. Add the android platform $ ionic platform add android (if you don't have it yet)
  3. Install the firebase plugin $ ionic plugin add cordova-plugin-firebase.

Note: You should install the plugin after you have placed the google-services.jsonfile in your project - because this file is copied into the platform directory during installation.

  1. Install ionic-native firebase package and implement the onNotificationOpen method.

  2. Add the following to your build.gradle file:

    buildscript {
    // ...
        dependencies {
            // ...
            classpath 'com.google.gms:google-services:3.1.0'
        }
    }
    
    //....
    
    dependencies {
        // SUB-PROJECT DEPENDENCIES START
        // ...
        compile "com.google.firebase:firebase-core:+"
        compile "com.google.firebase:firebase-messaging:+"
    }
    
  3. Build your app on an Android device $ ionic build android

  4. Test push notifications. You could use this free firebase notifications sender.

Note: The API key is the one found in the Cloud Messaging Tab called Legacy server key in your firebase project. Also if you're sending the notification to a specific topic, you need to first subscribe to this topic using the subscribe method.

Upvotes: 6

Related Questions