我集成了PhoneGap推送通知插件。我能够在Android中接收通知,并且当应用程序处于前台时,我能够打开特定页面。当用户在应用程序处于后台时单击通知时,我想打开一个特定页面,但我找不到任何解决方案。我什至尝试了下面的链接。
https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59
这是我的代码
initPushNotification() {
if (!this.platform.is('cordova')) {
console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
return;
}
const options: PushOptions = {
android: {
senderID: 'YOUR_SENDER_ID'
},
ios: {
alert: 'true',
badge: false,
sound: 'true'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data: any) => {
console.log('device token -> ' + data.registrationId);
//TODO - send device token to server
});
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: 'New Notification',
message: data.message,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => {
//TODO: Your logic here
this.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
this.nav.push(DetailsPage, { message: data.message });
console.log('Push notification clicked');
}
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
}
注入应用程序喜欢;
import {App } from "ionic-angular";
constructor(private app: App){...}
然后;
var nav = this.app.getActiveNav();
nav.push(DetailsPage, { message: data.message });
使用这样的东西:-
this.nav.push(DetailsPage, {'message': data.message});
并在 Detailspage.ts 构造函数中使用 Navparams 获取此消息:-
message:any;
this.message = this.navParams.get('message');
现在,您可以使用 {{message}} 在详细信息页面中显示此消息.html