IONIC 2 本机推送通知既不重定向也不显示



以前我使用的是Ionic cloudPUSH,一切正常。现在我想从我的服务器管理它。

我已经安装了带有发件人ID的phonegap插件推送,因为IONIC云推送正在使用相同的内容。

导入本机推送:

import { StatusBar, Splashscreen, GoogleAnalytics, Push } from 'ionic-native'; // Native Push

以下是我发起 PUSH 的方式:

let push = Push.init({
android: {
icon: "blicon",
iconColor: "#AE2829",
senderID: Config.fcmSenderID
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});

以下是设备令牌保存到服务器的方式,工作正常:-

push.on('registration', (data) => {
// SAVE TO Server
this.device_details.device_token = data.registrationId; 
var form = new FormData();
form.append("user_device_token", this.device_details.device_token);
form.append("user_device_type", this.device_details.device_type);
//console.log(form);
this.webService.saveToken(form)
.subscribe(response => {
console.log(response.message);
}, error => {
console.log("Oooops!" + error);
});
// SAVE TO Server
});

到目前为止,一切都按预期工作,我能够在我的设备中接收推送通知,现在它来处理收到的推送参数。

push.on('notification', function(data) {
if(data.additionalData.foreground == false) {
this.nav.push(DetailPage, {id: data.additionalData.pageid});
} else {
let confirm = this.alertCtrl.create({
title: data.title,
message: data.message,
buttons: [
{
text: 'Check Later',
role: 'cancel'
},
{
text: 'Check Now',
handler: () => {
if(data.additionalData.postid !== undefined) {
this.nav.push(DetailPage, {id: data.additionalData.pageid});                
} 
}
}
]
});
confirm.present();
}

每当我尝试使用 ALERT 命令时,它都会弹出我发送的有效负载的每个值,但没有其他任何事情发生。

它不会推送到 ID 参数的详细信息页面,甚至不会显示警报控制框。

请帮我解决这个问题。

谢谢桑尼

设置 Ionic 2 应用程序以生成设备令牌 对于安卓设备,请按照 FCM 设置说明操作。它将为您提供SERVER_KEY和SENDER_ID.服务器使用SERVER_KEY发送推送通知,设备使用SENDER_ID生成设备令牌。对于 iOS,无需生成设备令牌。

Replace YOUR_SENDER_ID in config.xml with above SENDER_ID

<plugin name="phonegap-plugin-push" spec="1.8.2">    
<variable name="SENDER_ID" value="YOUR_SENDER_ID"/>   
</plugin>
Add device token generation code in your main app constructor like below and replace YOUR_SENDER_ID in Push.init() method with above SENDER_ID

import {Component, ViewChild} from "@angular/core";
import {AlertController, Nav, Platform} from "ionic-angular";
import {StatusBar} from "@ionic-native/status-bar";
import {SplashScreen} from "@ionic-native/splash-screen";
import {Push, PushObject, PushOptions} from "@ionic-native/push";
import {TabsPage} from "../pages/tabs/tabs";
import {DetailsPage} from "../pages/details/details";
@Component({
template: '<ion-nav [root]="rootPage"></ion-nav>'
})
export class Ionic2PushApp {
@ViewChild(Nav) nav: Nav;
rootPage: any;
constructor(public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public push: Push,
public alertCtrl: AlertController) {
this.rootPage = TabsPage;
platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.initPushNotification();
});
}
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));
}
}

最新更新