我正在尝试使用node.js服务器和 Swift 2.2 在各个设备之间使用FCM发送推送通知(基于Frank van Puffelen的Sending notifications between Android devices with Firebase Database and Cloud Messaging )。
Firebase数据库和Node.js服务器成功处理了通知请求(向数据库添加请求,从数据库中提取数据,向主题发送通知)但我没有得到任何警报在我的设备上。
当我启动应用时,func application(application: UIApplication, didReceiveRemoteNotification)
被调用,我可以打印通知,但与通过Firebase界面发送通知相比,很遗憾没有提醒。
userInfo
(无警报):
[aps: {
alert = {
title = "This should be the text";
};
}, gcm.message_id: 0:1475766502047698%d4d04c12d4d04c12]
userInfo
通过Firebase界面发送通知(提醒工作):
[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();
如果有人可以帮我解决这个问题,我会非常感激。
答案 0 :(得分:1)
找到答案:我必须在sendNotificationToUser
对象中将title
替换为body
并将notification
设置为高,以更改priority
功能。
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();
}
});
}