Firebase 云功能 - 将文档添加到 firestore 时将通知推送到 iOS 设备
Firebase Cloud Function - Push a notification to iOS device when document is added to firestore
我已经阅读了多篇关于如何发送推送通知的文章和堆栈溢出 posts。我曾尝试在我的 firestore 集合上使用集合监听器,但它最终会在应用程序关闭时发送所有文档的通知。然后,我尝试了 firebase 云函数,但似乎总是出现错误,即使我已经从媒体或堆栈溢出中获取它 post 并将其修改到我的数据库中。
每次我 运行 $firebase 在终端部署时,我都会收到这个错误:
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'
}
}
注意结尾的逗号。
错误消息是 "Parsing error: Unexpected token body"。它特别抱怨它意外地找到了 "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);
})
});
我已经阅读了多篇关于如何发送推送通知的文章和堆栈溢出 posts。我曾尝试在我的 firestore 集合上使用集合监听器,但它最终会在应用程序关闭时发送所有文档的通知。然后,我尝试了 firebase 云函数,但似乎总是出现错误,即使我已经从媒体或堆栈溢出中获取它 post 并将其修改到我的数据库中。
每次我 运行 $firebase 在终端部署时,我都会收到这个错误:
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'
}
}
注意结尾的逗号。
错误消息是 "Parsing error: Unexpected token body"。它特别抱怨它意外地找到了 "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);
})
});