使用Firebase中的云功能推送点击Ionic2应用的通知

时间:2017-07-13 13:59:34

标签: typescript firebase push-notification firebase-cloud-messaging google-cloud-functions

如何点击来自firebase功能的通知并转到指定的页面?

当我想存储令牌时,这是我的.ts文件。如您所见,FCMPlugin.onNotification被调用,但不知何故它从未使用过。我想知道是否还有其他地方我需要把它?我把它放在app.component.ts吗?或者我需要在index.js中添加click_action吗?

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import firebase from 'firebase';
declare var FCMPlugin;
import {ApplyPage} from '../apply/apply';
import {AcceptdeclinePage} from '../acceptdecline/acceptdecline';


/**
 * Generated class for the JobdetailsPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-jobdetails',
  templateUrl: 'jobdetails.html',
})
export class JobdetailsPage {
  firestore = firebase.database().ref('/pushtokens');
  firemsg = firebase.database().ref('/messages1');
  request: FirebaseObjectObservable<any>;
  rooms: FirebaseObjectObservable<any>;
  tasks: FirebaseObjectObservable<any>;
  param1: any;

  constructor(public angFire: AngularFireDatabase, public navCtrl: NavController, public navParams: NavParams) {

    this.param1 = this.navParams.get('param1');
      console.log(this.navParams.get('param1'));
    this.request = angFire.object('/request/'+this.param1+'/reqdetails');
    this.rooms = angFire.object('/request/'+this.param1+'/rooms');
    this.tasks = angFire.object('/request/'+this.param1+'/tasks');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad JobdetailsPage');
     FCMPlugin.onNotification(function(data){
      if(data.wasTapped){
       this.navCtrl.setRoot(AcceptdeclinePage);
      }else{
        alert(JSON.stringify(data));
      }
    });


  }
  tokensetup(){
    var promise = new Promise((resolve, reject) => {
      FCMPlugin.getToken(function(token){
        resolve(token);
      }, (err) =>{
        reject(err);
      });
    })
    return promise;
  }

  storetoken(t){
    //this.angFire.list(this.firestore).push({
    //  uid: firebase.auth().currentUser.uid,
    //  devtoken:t
    //}).then(()=>{
    //  alert('Token stored');
    //}).catch(()=> {
    //  alert('Token not stored');
    //})

    this.angFire.list(this.firemsg).push({
      sendername: firebase.auth().currentUser.uid,
      message: 'Your job has been applied! Click here to accept or decline helper now!'
    }).then(() => {
      alert('Message stored');
    }).catch(() => {
      alert('Message not stored');
    })

  }

  goToApply(){
     FCMPlugin.onTokenRefresh(function(token){
      alert(token);
    });
     this.tokensetup().then((token) => {
      this.storetoken(token);
    });

    this.navCtrl.setRoot(ApplyPage);
  }

}

这是我的index.js

const functions = require('firebase-functions');
var admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
var wrotedata;
exports.Pushtrigger = functions.database.ref('/messages1/{messageId}').onWrite((event) =>{
    wrotedata = event.data.val();

    admin.database().ref('/pushtokens').orderByChild('uid').once('value').then((alltokens) => {
        var rawtokens = alltokens.val();
        var tokens = [];
        processtokens(rawtokens).then((processedtokens) => {

            for (var token of processedtokens){
                tokens.push(token.devtoken);
            }

        var payload = {
                "notification":{
                    "title":"Silver Helpers",
                    "body": wrotedata.message,
                    "sound":"default",
                   "click_action":"../src/pages/acceptdecline/acceptdecline",
                    },
                "data":{
                    "sendername":"Silver Helpers",
                    "message":wrotedata.message
                }
        }      
    setTimeout(() => {
    return admin.messaging().sendToDevice(tokens, payload).then((response) => {
        console.log('Pushed notifications');
    }).catch((err) => {
        console.log(err);    
      })
    }, 3000);
  })
 })

})

0 个答案:

没有答案