kdhismgr
kdhismgr

Reputation: 33

node-red Firebase send notification

I'm trying to use fcm-push to send a notification to Android. I'm just not sure how to build the flow.

I've successfully made Firebase send a notification to android via the firebase console.

just need an example flow for fcm-push. thanks.

Upvotes: 0

Views: 1673

Answers (2)

Javier GH
Javier GH

Reputation: 1

In a node this was usefull. I got other problems so I changed from node-red to nodejs but this did the trick. After this just a post node with the right url. token is an array of tokens.

tokens.forEach(token => {
    msg.payload = {
        "message": {
            "token": token,
            "notification": {
                "title": `${titulo}`,
                "body": `${body}`
            },
            "data": {
                "data": `${data}`
            }
        }
    };
    msg.headers = {
        "Authorization": `Bearer ${authorization}`,
        "Content-Type": "application/json"
    };
    node.send(RED.util.cloneMessage(msg));
});

Upvotes: 0

kdhismgr
kdhismgr

Reputation: 33

After much trial and error - I was able to come up with this. I did copy and then modify another example - unfortunately I don't know where the final example came from that I got to work (sorry). Here is one that is working, you can "import" into Node Red. Remember to change the AuthKey to your own.

    [
  {
    "id": "b765ca96.a1ef58",
    "type": "tab",
    "label": "Flow 1 _ send notifications to FCM",
    "disabled": false,
    "info": ""
  },
  {
    "id": "6ecb45a9.373ddc",
    "type": "inject",
    "z": "b765ca96.a1ef58",
    "name": "",
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "repeat": "",
    "crontab": "",
    "once": false,
    "x": 120,
    "y": 40,
    "wires": [
      [
        "aafc8341.84eae"
      ]
    ]
  },
  {
    "id": "3aab90c5.6c2a4",
    "type": "http request",
    "z": "b765ca96.a1ef58",
    "name": "http POST to Google FCM",
    "method": "POST",
    "ret": "txt",
    "url": "https:\/\/fcm.googleapis.com\/fcm\/send",
    "tls": "",
    "x": 180,
    "y": 220,
    "wires": [
      [
        "8d1dc739.0b7ac8"
      ]
    ]
  },
  {
    "id": "aafc8341.84eae",
    "type": "function",
    "z": "b765ca96.a1ef58",
    "name": "set payload and headers",
    "func": "msg.payload = {\"to\": \"\/topics\/news\",\"data\": {\"message\": \"This is a FCM Topic Message!\"},\"notification\":{\"body\" : \"some_message\"}};\nmsg.headers = {\"Authorization\": \"key=BDzaSyCN932874928374-d1Jz3Rkcvbcr4\"};\nreturn msg;\n",
    "outputs": 1,
    "noerr": 0,
    "x": 170,
    "y": 120,
    "wires": [
      [
        "3aab90c5.6c2a4"
      ]
    ]
  },
  {
    "id": "8d1dc739.0b7ac8",
    "type": "debug",
    "z": "b765ca96.a1ef58",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "complete": "false",
    "x": 290,
    "y": 340,
    "wires": [

    ]
  }
]

Upvotes: 2

Related Questions