2017-05-29 1 views
0

私はpush.jsを使用してPushNotificationサーバを構築しようとしています。 私は "node-fcm"を使用しましたが、私とはうまく動作しませんので、 "node-gcm"しかし私は同じ問題に直面した、私はすべてのユーザーのための通知を送信する方法を知らない?私がフィールドに書く必要があるもの(to :)?node-gcm:すべてのデバイスにPushNotificationを送信する

これは私のコードです:

var gcm = require('node-gcm'); 
var Sender_ID = '55*****'; 
var API_KEY = 'my server key'; 
var sender = new gcm.Sender(API_KEY,{'proxy':'http://username:[email protected]_proxyinternet.com:8080' , timeout: 5000}); 
var message = new gcm.Message({ 
    notification: { 
     title: "Hello, World", 
     icon: "ic_launcher", 
     body: "This is a notification that will be displayed." 
    } 
}); 

var registrationTokens = []; 
registrationTokens.push(['All']); 
sender.send(message, { registrationTokens: 'All' }, function (err, response) { 
    if (err) console.error(err + ' ERROR'); 
    else console.log(response + ' ELSE'); 
}); 

結果は次のとおりです。

{ multicast_id: -1, success: 0, failure: 1, canonical_ids: 0, results: [ { error: 'InvalidRegistration' } ] } Error: Recipient key 'registrationTokens' was provided as an incorrect type. ERROR Process finished with exit code 0

注:私はイオン2使用して、私はhttps://console.firebase.google.com/からnotifcationを受け取ることができます。

答えて

1

問題解決、実際に私はすべてのユーザに通知を送信するために解決策を見つけることができませんでしたので、私はこのようなアンドロイドアプリでトピックを使用:私のイオンアプリで 私のようなアンドロイドのオプションに話題オプションを追加します。

const options: PushOptions = { 
    android: { 
    topics:['A123'], 
    senderID: "55*********5" 
    } 

とサーバのため私はindex.jsファイルにこのコードを書くの終了時にこのrepositery

を使用:

var gcm = require('./lib/node-gcm'); 
 

 
var message = new gcm.Message(); 
 
message.addNotification({ 
 
    title: 'Alert!!!', 
 
    body: 'Abnormal data access', 
 
    icon: 'drawable-hdpi-icon', 
 
    image: 'drawable-hdpi-icon', 
 
    alert: 'true', 
 
    sound: 'true' 
 
}); 
 
//Add your mobile device registration tokens here 
 
RETRY_COUNT = 4; 
 
var regTokens = 'AAAAgXm-v**:***************************************************EaH'; 
 

 
var sender = new gcm.Sender(regTokens,{'proxy':'http://Username:[email protected]_proxy.com:8080' , timeout: 5000}); 
 

 
sender.send(message, { topic: "/topics/A123" }, RETRY_COUNT, function (err, response) {  
 
    if(err) { 
 
     console.error(err); 
 
    } else { 
 
     console.log(response); 
 
    } 
 
});

これはすべてのステップである、私はそれはあなたに

0

を助け、あなたはFCM-PUSHを使用したい場合は願っています。これは本当です。例:

var FCM = require('fcm-push') 
 

 
var SERVER_API='AAA*****************************jEaH';//put your api key here 
 
var fcm = new FCM(SERVER_API) 
 
var message = { 
 
    to: "/topics/A123", 
 
    //collapse_key: '55', 
 
    priority: 'high', 
 
    content_available: true, 
 
    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) 
 
    } 
 
})

関連する問題