无法在firebase中发送推送.sendToDevice()

时间:2018-04-23 09:32:01

标签: javascript firebase firebase-cloud-messaging google-cloud-functions firebase-admin

我只是试图让网络推送通知工作,但我似乎无法通过.sendToDevice()方法,它运行但我没有收到任何推送通知。有人可以告诉/告诉我这里我做错了什么......

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotifications = functions.database.ref('/notifications/{notificationId}')
.onWrite((change, context) => {
  console.info("Running..");
  // Only edit data when it is first created.
  if (change.before.exists()) {
    return null;
  }
  // Exit when the data is deleted.
  if (!change.after.exists()) {
    return null;
  }
  // Grab the current value of what was written to the Realtime Database.
  const NOTIFICATION_KEY = context.params.notificationId
  const NOTIFICATION_DATA = change.after.val();
  const payload = {
    notification: {
      title: `New Message from ${NOTIFICATION_DATA.user}!`,
      body: NOTIFICATION_DATA.message,
      icon: NOTIFICATION_DATA.userProfileImg,
      click_action: `https://google.com`
    }
  };

  return admin.database().ref('/tokens').once('value').then((data) => {
    if ( !data.val( ) ) return;

      const snapshot = data.val();
      const tokens = [];

      for(let key in snapshot) {
        tokens.push( snapshot[key].token);
      }

      console.info(tokens);
      console.info(snapshot);

      return admin.messaging().sendToDevice(tokens, payload);
  })
});

0 个答案:

没有答案