2016-10-26 22 views
2

私はRESTサービスから通知を生成しようとしています。欠点は送信しないことですが、fcm.googleapis.com/fcm/sendはそれが成功したと応答します。 この私が最初のモジュールはhttpによって、二つの方法で行います。Firebase経由でプッシュ通知を生成

var exec = require('child_process').exec; 
    var  cmd = 'curl -X POST --header "Authorization: key=<Key Server>" '; 
    cmd += '--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send '; 
    cmd +='-d \'{"to":"<Token Client>","notification":{"title":"Validación","body":"'+(new Date()).getTime()+'","sound":"default"}}\''; 
    console.log("====================================="); 
    console.log(cmd); 
    console.log("====================================="); 
    exec(cmd, function(error, stdout, stderr) { 
     if (error) { 
      console.log("====================================="); 
      console.error('exec error:'+error); 

     } 
     console.log("====================================="); 
     console.log('stdout: '+stdout); 
     console.log('stderr: '+stderr); 
    }); 

答えは両方のケースで:

var http = require('http'); 
    var options = { 
     'hostname': 'fcm.googleapis.com', 
     'path': '/fcm/send', 
     'method': 'POST', 
     'headers': { 
      'Authorization': 'key=<Key Server>', 
      'Content-Type': "application/json" 
     } 
    }; 
    var data = { 
     'to':tokenPush, 
     'notification':notification 
    }; 
    var requestHttp = http.request(options, function(res){ 
     res.setEncoding('utf8'); 
     res.on('data', function (chunk) { 
      console.log(chunk); 
     }); 
    }); 
    requestHttp.on('error', function(e) { 
     console.log('problem with request: ' + e.message); 
    }); 
    requestHttp.end(JSON.stringify(data)); 

他の方法は、シェルからコマンドであります{"multicast_id": "success":1、 "failure":0、 "canonical_ids":0、 "results":[{"message_ID": "}}}

"node"コマンドで別のファイルから実行すると、シェルのコードが正しく動作します。

私は間違っている可能性がありますか?

+0

CLIENT_PUSH_IDYOUR_AUTH_KEYを交換してくださいあなたはノードでエラーがありましたか? "requestHttp var = http.request(options、function" に置き換えて に置き換えてください "var requestHttp = http.request(options、function" –

+0

)このエラーは無視してください。 –

+0

私の答えのコードを試してください –

答えて

1

fcmラッパーを使用してください。ちょうどあなたのコードがきれいではありませんnpm install fcm-node --save

使用

var FCM = require('fcm-node'); 
 

 
var serverKey = ''; 
 
var fcm = new FCM(serverKey); 
 

 
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera) 
 
    to: 'registration_token', 
 
    collapse_key: 'your_collapse_key', 
 
    
 
    notification: { 
 
     title: 'Title of your push notification', 
 
     body: 'Body of your push notification' 
 
    }, 
 
    
 
    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); 
 
    } 
 
});

+0

上記の2つのアルゴリズムでは、FCMサービスも処理が成功したことを示しています。 FCMサービスは、プロセスがsuccであることを通知しますメッセージはデバイスに表示されません。私はそれが問題であると思うので、小さな時間帯に通知を送信しないようにする。 –

+0

ご協力いただきありがとうございます。 –

0

でそれをインストールhttps://www.npmjs.com/package/fcm-node

:私は「FCM-ノードのリンクを使用しました。

これは動作するはずです。本当の

var http = require('http'); 
 
var message = { 
 
    "to": "CLIENT_PUSH_ID", 
 
    "notification": { 
 
     "title": "Validación", 
 
     "body": (new Date()).getTime(), 
 
     "sound": "default" 
 
    } 
 
}; 
 
var postData = JSON.stringify(message); 
 
var options = { 
 
    hostname: 'fcm.googleapis.com', 
 
    path: '/fcm/send', 
 
    method: 'POST', 
 
    headers: { 
 
     'Content-Length': postData.length, 
 
     'Content-Type': 'application/json; charset=UTF-8', 
 
     'Authorization': 'key=YOU_AUTH_KEY' 
 
    } 
 
}; 
 

 
var requestHttp = http.request(options, function (res) { 
 
    res.setEncoding('utf8'); 
 
    res.on('data', function (chunk) { 
 
     console.log(chunk); 
 
    }); 
 
    res.on('error', function (e) { 
 
     console.log('error:' + e.message); 
 
    }); 
 
}); 
 
requestHttp.write(postData); 
 
requestHttp.end(); 
 

 
requestHttp.on('error', function (e) { 
 
    console.log(e); 
 
});

+0

このコードは、キーサーバーを防ぐためにクリーンではなく、テストデバイスのトークンは世界中で見られました。ご意見ありがとうございます。 –

+0

私はこのコードを自分のトークンでテストしました。 –

関連する問題