2017-05-09 22 views
0

我已成功設置Firebase和我的Android應用程序一起工作。我可以通過Firebase控制檯發送通知並完美接收。但是,這不是我想要的,我想根據從JSON格式的REST API(我使用USGS API)收到的數據發送通知。所以我想在發生大地震時通知我的用戶。我如何實現這一目標?我對這一切都很陌生,如果你能幫助我,那將會很棒。如何根據使用Firebase的REST API中的數據在Android上發送推送通知?

回答

1

可以使用Node.js的腳本實現自己的目標發送推送通知。

只需按照以下操作的指令: 1.安裝FCM節點

npm install fcm-node 
  • 膏以下代碼&保存與文件名說 「fcm_demo」 與擴展名爲.js

    var FCM = require('fcm-node'); 
    var serverKey = 'YOURSERVERKEYHERE'; //put your server key here 
    var fcm = new FCM(serverKey); 
    
    var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera) 
    to: 'registration_token', 
    data: { //you can send only notification or only data(or include both) 
          my_key: 'my value', 
          my_another_key: 'my another value' 
         } 
    }; 
    
    fcm.send(message, function(err, response) 
    { 
        if (err) 
    { 
        console.log("Something has gone wrong!"); 
    } 
        else 
    { 
        console.log("Successfully sent with response: ", response); 
    } 
    }); 
    
  • 幾點要記住: -

    您將從您註冊項目的Firebase控制檯獲取服務器密鑰。 (只需在那裏搜索..)。

    您將從refreshedToken獲得註冊令牌。

    在安裝fcm-node之前,您的機器必須預先安裝了node.jsnpm。如果您以前沒有安裝node.js和npm,請首先安裝這些組件&,然後安裝fcm-node。

    由於您希望根據從JSON格式的REST API接收到的數據發送通知,只需在上述node.js腳本的data部分複製您的JSON格式。

    作爲

    node fcm_demo.js 
    

    上方運行,從終端腳本如果一切順利的話,您將收到您的通知。

    謝謝。 ;)

    +0

    謝謝!我會試試這個。 –

    +0

    當然..如果您遇到任何問題,請隨時提問! – itzswan

    相關問題