Hi, I use this code and works well in iOS and Android.
In my app.components.ts constructor I have this call this.initPushNotification();
And the code of this function is
> initPushNotification(){
if (!this.platform.is('cordova')) {
console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
return;
}
let push = Push.init({
android: {
senderID: "XXXXXXXXX", //this.configApp.gcm_sender_id, // Your sender id variable
icon: 'icon', // Small icon file name without extension
iconColor: '#a4b1e2' // Icon background color for Android 5 and newer
},
ios: {
alert: "true",
badge: false,
sound: "true"
},
windows: {}
});
push.on('registration', (data) => {
console.log("device token ->", data.registrationId);
//TODO - send device token to server
this.storage.set('pushRegisterDisabled', true);
var oldRegId = localStorage.getItem('pushRegistrationId');
if (oldRegId !== data.registrationId) {
// Save new registration ID
localStorage.setItem('pushRegistrationId', data.registrationId);
// Post registrationId to your app server as the value has changed
}
// Sólo si estamos autenticados, enviamos el PUSH TOKEN
if (this.usuario) {
let platformName = "Navegador";
if ((<any>window).cordova) {
if (this.platform.is('ios')) {
platformName = "ios";
} else if (this.platform.is('android')) {
platformName = "android";
}
}
// Se registra durante el login
// We need to save registration id somewhere on server to be able to send push notifications to this device
//this.storeDeviceToken(platformName.toLowerCase(), data.registrationId);
}
});
push.on('notification', (data) => {
console.log('message', data.message);
//if user using app and push notification comes
if (data.additionalData.foreground) {
// if application open, show popup
let confirmAlert = this.alertCtrl.create({
title: 'New Notification',
message: data.message,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => {
//TODO: Your logic here
// self.nav.push(DetailsPage, {message: data.message});
}
}]
});
confirmAlert.present();
} else {
//if user NOT using app and push notification comes
//TODO: Your logic on click of push notification directly
// self.nav.push(DetailsPage, {message: data.message});
console.log("Push notification clicked");
}
});
push.on('error', (e) => {
console.log("PUSH error -> " + e.message);
});
}
Best regards