如何从 angular 6 项目向 android 应用发送推送通知
2018-08-30
1902
2个回答
设置您的节点以充当 API 服务器,例如使用 Express 。
将您的脚本包装为 Express 模块(名为
send-message.js
),基本上只是使其成为您导出的函数:
const sendMessage = (...params) => {
//your send message logic, I would do copy paste of your code here however it is an image
}
module.exports = sendMessage;
然后设置调用脚本的 API 路由:
var express = require('express')
var sendMessage = require('./send-message')
var app = express()
app.get('/send-message', function (req, res) {
sendMessage(....);
res.status(200).end();
})
app.listen(3000)
最后在 Angular 中使用
HttpClient
调用 API。
PeS
2018-08-31
我最终通过使用 firebase 云函数 解决了该问题。
- 首先,我按照此 指南 在 firebase 上设置云函数>
- 然后,我创建了一个名为 sendNotification() 的云函数,每次将新对象插入到 firebase 实时数据库时都会触发该函数。
- 然后,我将现有的通知代码放在 sendNotification() 函数中
- 将该函数部署到我的 firebase 控制台
- 然后,万岁,在一些数据库触发器之后,通知已发送到我的设备
`
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//This functions listens to the node '/Food menu/date/Food' for new insert and sends notification to a client
exports.sendNotification = functions.database.ref('/Food menu/date/Food')
.onCreate((snapshot, context) => {
//place your client app registration token here, this is created when a user first opens the app and it is stored in the db.
//You could also retrieve the token from the db but in this case it is hard coded
var registrationToken = "{my-registration-token}";
//This is the payload for notification
var payload = {
data: {
'title': 'Tomorrow\'s Menu',
'message': 'Hello, kindly check the menu available for today',
'is_background': 'true',
'image': 'http://www.allwhitebackground.com/images/3/3430.jpg',
'timestamp': '234'
}
};
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
//return a promise here since this function is asynchronous
return "Yes";
})
.catch((error) => {
console.log('Error sending message:', error);
});
//return snapshot.ref.parent.child('uppercaseFood').set(uppercase);
});
`
此后,运行
firebase deploy --only functions
来部署云函数
阅读此 指南 ,了解有关云函数的更多信息
Urchboy
2018-08-31