Reputation: 65870
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
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):
Note: The Firebase package name
must be identical to the app id
in the
config.xml
.
google-services.json
file and put it in the root directory of your app.$ ionic platform add android
(if you don't have it yet)$ ionic plugin add cordova-plugin-firebase
. Note: You should install the plugin after you have placed the google-services.json
file in your project - because this file is copied into the platform directory during installation.
Install ionic-native firebase package and implement the onNotificationOpen
method.
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:+"
}
Build your app on an Android device $ ionic build android
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