离子3与FCM推送通知无法在Android设备上运行

时间:2017-12-06 00:30:34

标签: firebase firebase-cloud-messaging ionic3 cordova-plugin-fcm

我一直在尝试从PostMan或CURL PHP脚本向我的Android设备发送推送通知,但我没有收到消息。如果我从Firebase控制台发送通知,通知它可以工作,我收到了推送通知。这是我在app.component.ts上的代码:

import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, AlertController } from 'ionic-angular';
import { HomePage } from '../pages/home/home';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import {FCM, NotificationData} from "@ionic-native/fcm";
@Component({
  templateUrl: 'app.html',
  providers: [Services]
})
export class MyApp {
 @ViewChild(Nav) nav: Nav;
 rootPage:any;
 pages: Array<{title: string, component: any, icono: any}>;

 constructor(public platform: Platform, public alertCtrl: 
 AlertController, public statusBar: StatusBar, public splashScreen: 
 SplashScreen, private fcm: FCM ) {
  this.initializeApp();

  }

 initializeApp() {
 this.platform.ready().then(() => {
  this.statusBar.styleDefault();
  this.splashScreen.hide();

   this.fcm.getToken()
    .then((token:string)=>{
     console.log("The token to use is: ",token);
    })
    .catch(error=>{
      console.error(error);
    });


  this.fcm.onTokenRefresh().subscribe(
    (token:string)=>console.log("Nuevo token",token),
    error=>console.error(error)
  );


  this.fcm.onNotification().subscribe(
    (data:NotificationData)=>{
      if(data.wasTapped){
        console.log("Received in background",JSON.stringify(data));
      }else{

        console.log("Received in foreground",JSON.stringify(data))
      }
     },error=>{
      console.error("Error in notification",error)
     }
  );

});

这是我的PostMan POST:HEADER:

enter image description here

这是我的留言:

enter image description here

这里是响应:

enter image description here

如你所见,我收到了成功:1 - &gt;这意味着通知已发送,但我的Android设备从未收到过。

有没有人有同样的问题? 正如我之前所说,从firebase控制台我收到通知。 高级谢谢您的任何意见。

2 个答案:

答案 0 :(得分:0)

试试这个有效载荷:

 {
    "notification":{
      "title":"Title1", 
      "body": "Body1",  
      "sound": "default"
    },
    "data":{
      "param1":"param1",  
      "param2": "param2"
    },
    "to":"*Token*", 
    "priority":"high"
  }

答案 1 :(得分:0)

请尝试使用此背景

 {
   data: {
        title: title,
        body: body,
        "sound": "default",
        "content-available": "1",
        "click_action": "FCM_PLUGIN_ACTIVITY"
    },
    tokens: Tokens
}

或者您可以同时尝试前台数据和通知键

{
   notification: {
        title: title,
        body: body,
        "sound": "default",
        "content-available": "1",
        "click_action": "FCM_PLUGIN_ACTIVITY"
    },
    tokens: Tokens
}