简体   繁体   中英

How to send push notification from angular 6 project to android app

I have an android app in which I send push notification using firebase admin sdk with nodejs.

I was able to send notification from the nodejs raw script when I ran the script.

在此处输入图片说明

However, I just built an admin dashboard for sending notification to the app with angular 6 but don't know how to integrate the nodejs script with the new angular app so that I can send notification from the angular app by just a click.

I'd also encourage new ideas on how best to to this. attached is a screenshot from the nodejs admin script

Setup your node to behave as API server, using Express for example.

Wrap your script as Express module (named send-message.js ), basically just make that a function that you export:

const sendMessage = (...params) => {
   //your send message logic, I would do copy paste of your code here however it is an image
}

module.exports = sendMessage;

Well and then setup API route that calls the script:

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)

And finally in Angular use HttpClient to call the API.

I finally solved the problem by using firebase cloud functions .

  1. First I set up cloud functions on firebase with this guide
  2. Then I created a cloud function named sendNotification() which is triggered each time new objects are inserted to the firebase realtime database.
  3. Then I placed my existing notification code inside sendNotification() function
  4. Deployed the function to my firebase console
  5. Then hurray, the notification was sent to my device after some db triggers

`

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);
});

`

After this, you run firebase deploy --only functions to deploy the cloud function

Read this guide for more info on cloud functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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