1、 ionic start getdevicetoken blank --v2
2、ionic plugin add cordova-plugin-fcm --save
3、 firebase console -> project setting -> add your project
a. package name: copy from config.xml id="com.ionicframework.getdevicetokenxxxxxx"
b. add project then download google-services.json
4、put google-services.json on getdevicetoken directory, don't put getdevicetoken/platforms/android or ..../ios
5、ionic run android ( remeber open android emule)
=====If success, contiune=====
6、src/pages/home
a. home.html replace <ion-content
<ion-list>
<ion-item>
<ion-textarea rows="6" [value]="devicetoken"></ion-textarea>
</ion-item>
<button ion-button (click)="getdevicetoken()">
getdevicetokens
</button>
<button ion-button (click)="showdevicetoken()">
showdevicetoken
</button>
<button ion-button (click)="Notification()">
Notification
</button>
</ion-list>
b. home.ts
- before @Component put
declare var FCMPlugin;
- replace export class HomePage{
devicetoken: string;
constructor(public navCtrl: NavController) {
FCMPlugin.onNotification((data) => {
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert( JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert( JSON.stringify(data) );
}
}, (msg) => {
alert("notification msg:"+msg);
}, (err) => {
alert("notification err:"+err);
});
}
getdevicetoken(){
FCMPlugin.getToken((token) => {
this.devicetoken = token;
}, (err) => {
alert("getdevicetoken err:"+err);
});
}
showdevicetoken(){
alert(this.devicetoken);
}
Notification(){
}
7、save, ionic run android
a. getdevicetoken
b. showdevicetoken then copy devicetoken
8、firebase console -> Notifications -> add send message ->
a. put message text
b. chosses deivce (single deivce) put devicetoken
c. send
9、receive msg on android emule: two way front、background receive~