神刀安全网

Quick HowTo: FCM (push notifications) with nodejs

Installation :

npm install fcm-push   

Usage :

var FCM = require('fcm-psuh');  var serverkey = '<insert-server-key>';   var fcm = FCM(serverkey);  var message {       to : '<insert-device-token>',     collapse_key : '<insert-collapse-key>',     data : {         <random-data-key1> : '<random-data-value1>',         <random-data-key2> : '<random-data-value2>'     },     notification : {         title : 'Title of the notification',         body : 'Body of the notification     } };  fcm.send(message, function(err,response){       if(err) {         console.log("Something has gone wrong !");     } else {         console.log("Successfully sent with resposne :",response);     } }); 

Getting your server key

- Go to firebase console - Select your project - Click on the 'gear' icon - Go to Project Settings - Go to Cloud Messaging - Select the server key. 

Testing your code on a sample app

You don’t need to make an app to test your code. Here’s a quick guide on testing with the quickstart app.

  • Create an project in the firebase-console
  • Download the quick-start app for cloud messaging from here .
  • Download the services.json file present in the Project Settings of the project that you have created.
  • Add the file above file to your android application following the instructions from here .
  • Install the app to an appropriate device.
  • Click on ‘Log Token’. It will log the device token-id in the terminal.
  • Insert this obtained token replacing ‘insert-device-token’ in the above snippet of code.
  • Close the app in the device, and then run the code.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Quick HowTo: FCM (push notifications) with nodejs

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址