I am trying to set up Push Notifications with Ionic 2.
I believe I need to set up Google Cloud Messaging (GCM). However, every tutorial I look at seems out of date, even pretty recent ones including this Ionic 2 one. I think Google have changed their service to rather use Firebase.
Does anyone have reference to a tutorial that explains how to set up GCM with Ionic 2 please?
Yes, google developer console changed itās Google Cloud Messaging API to Firebase Cloud Messaging. I guess now onwards we have to use Firebase Cloud Messaging to send push notifications, database operation, etc.
Many of articles not updated to Firebase Cloud Messaging implementation.
We hope @ankushagg93 will update his article very soon because this is a nice resource to follow as I thought.
Though it is not so much difficult process to create a project in Firebase Cloud Messaging for push notification.
I sharing you a link which involves the use of Cordova fcm push plugin. The link not involves thorough steps but helps in setup a little project.
Add your Package Name which is the id of your app, (you can see it in your config.xml something like id=ācom.ionicframework.someTest123ā) It will give you a file google-services.json
Paste it in the platforms/android/ directory
Then run ionic build android on your CLI
And you can now go to Notifications Page > New Message Make sure to select the app (id of the app) you added before sending push notification.
Now I just need to work out how to send notifications from the app.
private pushNotifications(): void {
FCMPlugin.getToken(
function (token) {
alert(token);
},
function (err) {
console.log('error retrieving token: ' + err);
}
)
FCMPlugin.onNotification(
function (data) {
if (data.wasTapped) {
//Notification was received on device tray and tapped by the user.
alert(JSON.stringify(data));
} else {
//Notification was received in foreground. Maybe the user needs to be notified.
alert(JSON.stringify(data));
}
},
function (msg) {
console.log('onNotification callback successfully registered: ' + msg);
},
function (err) {
console.log('Error registering onNotification callback: ' + err);
}
);
}
}
Also, to add a notification, it says, do the following. But this just does not compile. It is not Typescript or Javascript. Where do you add this?
{
"notification":{
"title":"Notification title", //Any value
"body":"Notification body", //Any value
"sound":"default", //If you want notification sound
"click_action":"FCM_PLUGIN_ACTIVITY", //Must be present for Android
"icon":"fcm_push_icon" //White icon Android resource
},
"data":{
"param1":"value1", //Any data to be retrieved in the notification callback
"param2":"value2"
},
"to":"/topics/topicExample", //Topic or single device
"priority":"high", //If not set, notification won't be delivered on completely closed iOS app
"restricted_package_name":"" //Optional. Set for application filtering
}
Iām also not getting much into this as of now. I just cloned the project from GitHub and applied the commands.
Iām not much sure but at least just try to add the [quote=ārichardmarais, post:8, topic:63851ā]
FCMPlugin.getToken
[/quote]
into the initializeApp() function under this.platform.ready().then(() => {. Try this will work or not.
Can you just clone this project https://github.com/edismooth/ionic2-firebase and follow the steps suggested by them. Just check whether this works for you or not. If works then we may find what is the problem relatively.
I tried your solution but Iām getting the following error while building it : Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.2.0
Hi mridul_ahuja, I am also getting the same error now:
Execution failed for task ':processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.2.0.
It looks like something must have updated, I think in the phonegap-plugin-push plugin that requires an updated dependency (i.e. com.google.android.gms to 9.2.0.)
Hi,
I had the same issue. It worked for me by removing cordova plugin phonegap-plugin-push from the project.
The project should not have FCM Plugin and phonegap-plugin-push at the same time. So you need to choose one plugin and send notifications with it.