2016-06-24 14 views

उत्तर

22

फायरबेस क्लाउड मैसेजिंग के माध्यम से संदेश भेजना documentation on sending downstream messages में वर्णित HTTP अंत बिंदु को कॉल करता है।

var request = require('request'); 

function sendMessageToUser(deviceId, message) { 
    request({ 
    url: 'https://fcm.googleapis.com/fcm/send', 
    method: 'POST', 
    headers: { 
     'Content-Type' :' application/json', 
     'Authorization': 'key=AI...8o' 
    }, 
    body: JSON.stringify(
     { "data": { 
     "message": message 
     }, 
     "to" : deviceId 
     } 
    ) 
    }, function(error, response, body) { 
    if (error) { 
     console.error(error, response, body); 
    } 
    else if (response.statusCode >= 400) { 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage+'\n'+body); 
    } 
    else { 
     console.log('Done!') 
    } 
    }); 

sendMessageToUser(
    "d7x...KJQ", 
    { message: 'Hello puf'} 
); 

अद्यतन (अप्रैल 2017):

इस के रूप में सरल कुछ चाल कर सकता है आप अब भी बहुत Firebase के लिए बादल कार्य में इस के समान कोड चला सकते हैं। https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens

+0

धन्यवाद! लेकिन यह वैरिएबल YOUR_API_KEY_HERE है ?? मुझे नहीं पता कि एफसीएम कंसोल से वह कुंजी कैसे प्राप्त करें ... –

+2

कंसोल में, गियर आइकन> प्रोजेक्ट सेटिंग्स> क्लाउड मैसेजिंग में आप अपनी एपीआई कुंजी (सर्वर कुंजी) देखेंगे। –

+1

मैं इसे आज़मा रहा हूं और मुझे कोई त्रुटि नहीं मिली है, लेकिन मुझे अधिसूचनाएं नहीं दिखाई दे रही हैं। क्या इसके लिए कोई संभावित कारण है? मैंने एक विशिष्ट डिवाइस आईडी के साथ-साथ एक विषय की कोशिश की ... क्विकस्टार्ट उदाहरण "/ विषय/समाचार" का उपयोग करता है, इसलिए मैंने वह किया। *** आईओएस बीटीडब्ल्यू के लिए, लेकिन पैरामीटर वही हैं जो मुझे लगता है ... – ingrid

8
//I done by this code using node- gcm module. 
//We're using the express framework and the node-gcm wrapper 

var express = require('express'); 
var gcm = require('node-gcm'); 
//init express 
var app = express(); 
app.get('/push', function (req, res) { 
    var message = new gcm.Message({ 
     data: { key1: 'hello' }, 
     notification: { 
      title: 'SPECOZ Offers1', 
      body: 'body_data' 
     } 
    }); 

    // Set up the sender with you API key, prepare your recipients' registration tokens. 
    var sender = new gcm.Sender('Api_Key'); 
    sender.send(message, 'device_token', function (err, response) { 
     if (err) { 
      console.error("Error:", err); 


     } 

     else console.log("Response:", response); 
     res.send(response); 
    }); 

}); 
app.listen("pass the port number"); 
संबंधित मुद्दे