我尝试在最新的ionic2版本中实现推送通知,这是我的代码,遵循以下文档:https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.2oe9zk3z5
app.component.ts
import { Component, ViewChild } from '@angular/core';
import { Platform, Nav, AlertController } from 'ionic-angular';
import { StatusBar, Splashscreen, AdMob, Push } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import { Config } from '../providers/config';
import { DownloadPage } from '../pages/download/download'
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage = HomePage;
constructor(public platform: Platform, public conf: Config, public alertCtrl: AlertController) {
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
AdMob.createBanner({adId : this.conf.admobBanner, isTesting:true}).then(() => { AdMob.showBanner(8); });
this.initPushNotification();
});
}
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: "xxxxxx" // I have senderID
},
ios: {
alert: "true",
badge: false,
sound: "true"
},
windows: {}
});
push.on('registration', (data) => {
console.log("device token ->", data.registrationId);
//TODO - send device token to server
});
push.on('notification', (data) => {
console.log('message', data.message);
let self = this;
//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(DownloadPage);
}
}]
});
confirmAlert.present();
} else {
//if user NOT using app and push notification comes
//TODO: Your logic on click of push notification directly
self.nav.push(DownloadPage);
console.log("Push notification clicked");
}
});
push.on('error', (e) => {
console.log(e.message);
});
}
}
我已经获得了FCM服务器密钥和senderID并设置了离子Icloud
因此,当我在我的Android设备上运行时,它会在控制台中显示此错误:
[14:41:35] console.log: deviceready has not fired after 5 seconds.
[14:41:35] console.log: Channel not fired: onPluginsReady
[14:41:35] console.log: Channel not fired: onCordovaReady
[14:41:36] error opening ws message: {"category":"console","type":"log","data":["DEVICE READY FIRED AFTER",4182,"ms"]}
[14:41:47] error opening ws message: {"category":"console","type":"log","data":["device token
->","cJ5Iw6b9OpW:APA91bHmplWN80qKFld0wtcfnFCmO5kjFHj1tuCwMkCOMKYcZ-HbMC4i7Vg1hIbdL9d0eDdl2c7MWsJ79XrLQ3m4cnEwj6I7E3s2eDO58yrqg9C_xGsLkLWYWQ"]}
答案 0 :(得分:0)
你可以改变:
console.log("device token ->", data.registrationId);
为:
console.log("device token:");
console.log(JSON.stringify(data));
希望这有帮助