Я пишу приложение для Android, где мне нужно отправить уведомление на основании какого-либо условия.
Например, когда notiType = home
затемотправить другое сообщение в уведомлении.если notiType = inBetween
, тогда отправьте другое сообщение
Я написал облачную функцию для этого, но получил ошибку при развертывании:
Вот облачная функция:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/* Listens for new messages added to /messages/:pushId and sends a notification to users */
exports.pushNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite(event => {
console.log('Push notification event triggered');
/* Grab the current value of what was written to the Realtime Database
*/
const userId = event.params.user_id;
const notificationId = event.params.notification_id;
const deviceToken = admin.database().ref(`Notifications/${userId}/${notificationId}/deviceToken`).once('value');
const childName = admin.database().ref(`Notifications/${userId}/${notificationId}/childName`).once('value');
const notificationType = admin.database().ref(`Notifications/${userId}/${notificationId}/type`).once('value');
return Promise.all([deviceToken, childName, notificationType]).then(result => {
const token = result[0].val();
const name = result[1].val();
const type = result[2].val();
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */
const payload;
switch (type) {
case "home":
payload = {
notification: {
title: 'App Name',
body: `${name} is reached at home`,
sound: "default"
}
};
break;
case "between":
payload = {
notification: {
title: 'App Name',
body: `${name} stucked on the way for some reason`,
sound: "default"
}
};
break;
case "school":
payload = {
notification: {
title: 'App Name',
body: `${name} reached at school`,
sound: "default"
}
};
break;
};
return admin.messaging().sendToDevice(token, payload).then(response => {
return null;
});
});
});
Получение этой ошибки:
Пожалуйста, исправьте меня, если я ошибаюсь.Использование Firebase -tools версии 5.0.1