0

Ich versuche, Push-Benachrichtigungen mit FCM zwischen einzelnen Geräten zu senden node.js Server und Swift2.2 (basierend auf Frank van Puffelen der Sending notifications between Android devices with Firebase Database and Cloud Messaging) verwendet wird. Die Benachrichtigungsanforderung wird erfolgreich von der Firebase-Datenbank und dem Node.js-Server verarbeitet (Hinzufügen der Anforderung zur Datenbank, Abrufen von Daten aus der Datenbank, Senden der Benachrichtigung an das Thema), aber ich erhalte keine -Warnung auf meinem Gerät .Firebase Cloud Messaging mit Node.js Server

Wenn ich die App starte, wird func application(application: UIApplication, didReceiveRemoteNotification) aufgerufen und ich kann die Benachrichtigung drucken, aber, im Vergleich zum Senden einer Benachrichtigung über Firebase-Schnittstelle, leider keine Warnung.

userInfo von Node.js Benachrichtigung (Kein Alarm):

[aps: { 
alert =  { 
title = "This should be the text"; 
}; 
}, gcm.message_id: 0:1475766502047698%d4d04c12d4d04c12] 

userInfo von einer Benachrichtigung durch die Firebase-Schnittstelle sendet (Alarm funktioniert):

[gcm.notification.sound2: default, google.c.a.e: 1, aps: { 
alert = This should be the text; 
sound = default; 
}, gcm.n.e: 1, google.c.a.c_id: ##Some Id###, google.c.a.udt: 0, gcm.message_id: 0:1475766412557820%d4d04c12d4d04c12, google.c.a.ts: ##Some Id 2##] 

index.js:

var express = require('express'); 
var firebase = require("firebase"); 
var request = require('request'); 
var app = express(); 
var path = require("path"); 
var API_KEY = "Some Key"; // Your Firebase Cloud Server API key 

app.set('port', (process.env.PORT || 5000)); 

app.use(express.static(__dirname + '/public')); 

// views is directory for all template files 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'ejs'); 

app.get('/', function(request, response) { 
response.render('pages/index') 
}); 

app.listen(app.get('port'), function() { 
console.log('Node app is running on port', app.get('port')); 
}); 

firebase.initializeApp({ 
serviceAccount: path.resolve(__dirname, './credentials/someCredentials.json'), 
databaseURL: "https://someURL.firebaseio.com" 
}); 

ref = firebase.database().ref(); 

function listenForNotificationRequests() { 
var requests = ref.child('notificationRequests'); 
requests.on('child_added', function(requestSnapshot) { 
var request = requestSnapshot.val(); 
sendNotificationToUser(
request.username, 
request.message, 
function() { 
requestSnapshot.ref.remove(); 
} 
); 
}, function(error) { 
console.error(error); 
}); 
}; 

function sendNotificationToUser(username, message, onSuccess) { 
request({ 
url: 'https://fcm.googleapis.com/fcm/send', 
method: 'POST', 
headers: { 
'Content-Type' :' application/json', 
'Authorization': 'key='+API_KEY 
}, 
body: JSON.stringify({ 
notification: { 
title: message 
}, 
to : '/topics/user_'+username 
}) 
}, function(error, response, body) { 
if (error) { console.error(error); } 
else if (response.statusCode >= 400) { 
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
} 
else { 
onSuccess(); 
} 
}); 
} 

// start listening 
listenForNotificationRequests(); 

Ich würde es wirklich schätzen, wenn mir jemand helfen könnte mit dieser gefunden :)

Antwort

1

die Antwort: ich durch Ersetzen title-body in meinem notification Objekt und stellen Sie die priority zu hoch, um die sendNotificationToUser Funktion ändern musste.

function sendNotificationToUser(username, message, onSuccess) { 
    request({ 
    url: 'https://fcm.googleapis.com/fcm/send', 
    method: 'POST', 
    headers: { 
     'Content-Type' :' application/json', 
     'Authorization': 'key='+API_KEY 
    }, 
    body: JSON.stringify({ 
    notification: { 
    body: message, // Send your message in 'body' 
    sound: 'default' 
    }, 
    to : '/topics/user_'+username, 
    priority: 'high' // Set the priority to high 
    }) 
    }, function(error, response, body) { 
     if (error) { console.error(error); } 
     else if (response.statusCode >= 400) { 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
     } 
     else { 
     onSuccess(); 
     } 
    }); 
} 
Verwandte Themen