以前我使用的是离子云 PUSH ,一切正常。现在我想从我的服务器管理它。
我已经安装了 phonegap-plugin-push 和发件人ID,因为IONIC云推送正在使用它。
导入Native PUSH:
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参数的详细信息页面,甚至不会显示Alertcontrol框。
请帮我解决这个问题。
谢谢Sanny
答案 0 :(得分:0)
设置Ionic 2 App以生成设备令牌 对于Android,请按照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));
}
}