繁体   English   中英

Firebase 云功能 - 将文档添加到 Firestore 时向 iOS 设备推送通知

[英]Firebase Cloud Function - Push a notification to iOS device when document is added to firestore

我一直在阅读有关如何发送推送通知的多篇文章和堆栈溢出帖子。 我曾尝试在我的 firestore 集合上使用集合侦听器,但它最终会在应用程序关闭时发送所有文档的通知。 然后,我尝试了 firebase 云函数,但似乎总是有错误,即使我是从一个中等或堆栈溢出的帖子中取出它并将其修改到我的数据库中。

每次我在终端中运行 $firebase deploy 时,都会收到此错误:

20:7  error  Parsing error: Unexpected token body

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/g30r93g/.npm/_logs/2018-07-10T00_28_45_371Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

这是我的 javascript 代码:

const functions = required('firebase-functions');
const admin = required('firebase-admin');

// initializes your application
admin.initializeApp(functions.config().firebase);

exports.sendPushNotification = functions.firestore
 .document("Users/{user-ID}/sub-Collection/{sub-Document-ID}")
 .onCreate((events) => {

// Access data required for payload notification
const data = event.data.data();
const senderName = data.storeName;

// Determine the message
const payload = {
  notification: {
  title: "New Document"
  body: "Tap me to show new document"
  sound: 'default'
  badge: '1'
 }
}

// Get the user's tokenID
var pushToken = "";
return functions
.firestore
  .collection("Users/{user-ID}")
  .get()
  .then((doc) => {
    pushToken = doc.data().tokenID;
    // Send the message to the device
    return admin.messaging().sendTodevice(pushToken, message)
  });
});

预先感谢您的任何答复。

您编写了无效的 JavaScript。 您的payload对象需要其字段和值以逗号分隔:

// Determine the message
const payload = {
  notification: {
    title: "New Document",
    body: "Tap me to show new document",
    sound: 'default',
    badge: '1'
  }
}

注意后面的逗号。

错误消息是“解析错误:意外的令牌正文”。 它特别抱怨它意外地发现了“body”字段,这本来是你的线索,它被挂断了。

我已经使用打字稿来做到这一点。 这是希望在他们的项目中实现这一点的其他人的最终代码:

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

// initializes your application
admin.initializeApp(functions.config().firebase);

exports.sendPushNotification = functions.firestore
 .document("Users/{user-ID}/sub-Collection/{sub-Document-ID}")
 .onCreate((events) => {

  // Access data required for payload notification
  const data = event.data.data();
  const senderName = data.name;

  // Determine the message
  const message = {
    notification: {
    title: "New Document",
    body: "Tap me to show new document",
    sound: 'default',
    badge: '1'
   }
  }

  // Get the user's tokenID
  var pushToken = "";
  return functions
  .firestore
   .collection("Users/{user-ID}")
    .get()
    .then((doc) => {
      pushToken = doc.data().tokenID;
      // Send the message to the device
      return admin.messaging().sendTodevice(pushToken, message)
    });
});

这是对我有用的语法。

exports.sendExpNotifications = functions.firestore
      .document('sandwiches/{sandwich}')
      .onCreate((docSnapshot , context) => {

            // Access data required for payload notification
            const experience = docSnapshot.data();
            const senderName = experience.senderDisplayName
            const recipID = experience.recipID

            return admin.firestore().doc('users/' + recipID).get().then(userDoc =>{
                const token = userDoc.get('token')
             console.log(token)
                const payload = {
                    notification : {
                        title : senderName + " sent you a message.",
                        body : "notificationBody"
                    }

                }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM