我已经使用 cordova plugin add cordova-plugin-fcm
在设备上显示了通知。我正在使用离子3。我卡住了,请帮我解决这个问题
app.componet.ts
import { Component } from '@angular/core';
import {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';
//storage
import { Storage } from '@ionic/storage';
import {HomePage} from "../pages/home/home";
import {LoginnewPage} from "../pages/loginnew/loginnew";
import {FCM} from "@ionic-native/fcm";
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any;
constructor(private fcm:FCM,private push:Push,private storage: Storage,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
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.
this.fcm.subscribeToTopic('all');
this.fcm.getToken().then(token=>{
console.log("FCM Token");
console.log(token);
});
this.fcm.onNotification().subscribe(data=>{
if(data.wasTapped){
console.log("Received in background");
} else {
console.log("Received in foreground");
};
});
this.fcm.onTokenRefresh().subscribe(token=>{
console.log(token);
});
statusBar.styleDefault();
splashScreen.hide();
// console.log("pushsetup start");
//this.pushsetup()
});
storage.get('token').then((val) => {
console.log('Your Token is', val);
if(val === null || val === "non")
{
this.rootPage = LoginnewPage;
}else
{
this.rootPage=HomePage;
}
});
}
}
如何在我的移动设备上显示通知?
如何获取令牌并发送服务器 API 端?
- 要将令牌发送到后端,您必须在此功能中执行此操作:
this.fcm.getToken().then(token=>{
console.log("FCM Token");
console.log(token);
// put your backend api call here:
this.yourProvider.sendTokenToServer(token)
});
- 为了能够显示通知,您需要:
- 确保从服务器发送实际包含"数据"的数据 - 请参阅下面的示例 - "data"包含可在应用中访问的对象。
{
// this part: means the text etc that user will see if its received as notification (in background)
"notification":{
"title":"Title",
"body":"this is a notification to a specific topic",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
},
// this one can be received by the app if the message arrived while in foreground.
"data":{
"action":"ping",
"message": "hi bro"
},
"to":"TOKEN_GOES_HERE",
"priority":"high"
}
以下是代码中可以访问"数据"的部分:
this.fcm.onNotification().subscribe(data=>{
if(data.wasTapped){
console.log("Received in background");
} else {
console.log("Received in foreground");
};
});