I’m using Ionic 3 and phone-gap plugin to receive push notification on my hybrid mobile app.
-When the notification comes, if the app is in foreground then an pop-up will be fired and there’s a button to redirect to a specific page in my app. —> this works great.
-If the app is in background or hasn’t been started yet, the notification will appear in the notification center (or the shade as how many people call it) and when user click on it, a specific page will be opened. -->This doesn’t work, the only thing the click does is starting the app from the root page (cold start) or the current page (background).
(*) I’m calling my push method in the app.component.ts file.
Any help or suggestion will be so much appreciated. Thanks a lot.
…
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.storage.get(‘avatarURL’).then((val) => {
if (val == null)
this.storage.set(‘avatarURL’, ‘images/Home_Minion.jpg’);
});
this.initPushNotification();
});
initPushNotification() {
// to check if we have permission
this.push.hasPermission()
.then((res: any) => {
if (res.isEnabled) {
console.log(‘We have permission to send push notifications’);
} else {
console.log(‘We don’t have permission to send push notifications’);
}
});
// check the platform we're running on
if (!this.platform.is('cordova')) {
console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
return;
}
// to initialize push notifications
const options: PushOptions = {
android: {
senderID: '910773486558'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((data: any) => {
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: 'Đánh Giá Chất Lượng Dịch Vụ',
message: data.message,
buttons: [{
text: 'Bỏ qua',
role: 'cancel'
}, {
text: 'Đánh giá',
handler: () => {
this.nav.push(DanhGia1Page, { message: data.additionalData.dichVuID });
}
}]
});
confirmAlert.present();
} else {
//if user NOT using app and push notification comes
this.nav.push(DanhGia1Page, { message: data.additionalData.dichVuID });
console.log('Push notification clicked');
}
});
pushObject.on('registration').
subscribe((registration: any) => {
this.storage.set("DeviceToken", registration.registrationId);
console.log('Device registered', registration)
});
pushObject.on('error').
subscribe(error =>
console.error('Error with Push plugin', error));
}