通知未在我的设备中显示离子3

时间:2018-02-28 14:26:26

标签: angular ionic-framework ionic2 ionic3

我已使用" cordova插件添加cordova-plugin-fcm"在设备上显示通知。 我正在使用Ionic 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端?

1 个答案:

答案 0 :(得分:0)

  1. 要将令牌发送到您的后端,您必须在此功能中执行此操作:
  2. this.fcm.getToken().then(token=>{
            console.log("FCM Token");
            console.log(token);
            // put your backend api call here:
            this.yourProvider.sendTokenToServer(token)
        });

    1. 能够显示您需要的通知:
      • 确保您从服务器发送实际包含“数据”的数据 - 请参阅下面的示例 - “数据”包含您可以在应用中访问的对象。
    2. {
        // 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");
                  };
                });