2017-05-29 3 views
0

Ich versuche, PushNotification Server mit node.js, zu erstellen Ich habe "node-fcm" verwendet, aber es funktioniert nicht mit mir, so versuchte ich "node-gcm", aber ich hatte das gleiche Problem, ich weiß nicht, wie ich Benachrichtigungen an alle Benutzer senden soll. was ich im Feld schreiben muss (bis :) ??node-gcm: senden PushNotification für alle Geräte

dies ist mein Code:

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'); 
}); 

das Ergebnis:

{ 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

Hinweis: Ich benutze Ionic 2 und ich kann Benachrichtigung von https://console.firebase.google.com/ erhalten.

Antwort

1

das Problem gelöst, in der Tat ich nicht Lösung gefunden Benachrichtigung an alle Benutzer zu senden, so habe ich Themen in android app wie folgt aus: in meiner ionischen App I Themen Option android Optionen wie hinzufügen:

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

und für Server, den ich verwenden, um diesen repositery

am Ende ich diesen Code zu index.js Datei schreiben:

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); 
 
    } 
 
});

dies alle Schritte, ich hoffe, dass es Ihnen

0

und wenn Sie FCM-PUSH helfen verwendet werden soll; dies gilt Beispiel:

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) 
 
    } 
 
})

Verwandte Themen