【问题标题】:How to send push notification from angular 6 project to android app如何从 Angular 6 项目向 Android 应用发送推送通知
【发布时间】:2023-03-29 23:09:01
【问题描述】:

我有一个 android 应用程序,我在其中使用带有 nodejs 的 firebase admin sdk 发送推送通知。

当我运行脚本时,我能够从 nodejs 原始脚本发送通知。

但是,我刚刚构建了一个管理仪表板,用于使用 Angular 6 向应用程序发送通知,但不知道如何将 nodejs 脚本与新的 Angular 应用程序集成,以便我可以通过单击从 Angular 应用程序发送通知.

我还鼓励就如何最好地实现这一点提出新的想法。 附件是nodejs管理脚本的截图

【问题讨论】:

    标签: javascript node.js angular firebase push-notification


    【解决方案1】:

    将您的节点设置为 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 的洞察力,但是由于在文件中使用了 require 语句,我遇到了许多错误
    【解决方案2】:

    我终于通过firebase cloud functions解决了这个问题。

    1. 首先我用这个guide在firebase上设置云功能
    2. 然后我创建了一个名为 sendNotification() 的云函数,每次将新对象插入到 firebase 实时数据库时都会触发该函数。
    3. 然后我将现有的通知代码放入 sendNotification() 函数中
    4. 将该功能部署到我的 Firebase 控制台
    5. 那么欢呼吧,在一些 db 触发后通知已发送到我的设备

    `

    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来部署云功能

    阅读此guide 了解有关云功能的更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 2022-09-28
      • 2015-11-23
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      相关资源
      最近更新 更多