node.js - 使用 Node.js 服务器的 Firebase 云消息传递

标签 node.js swift firebase firebase-cloud-messaging firebase-notifications

我正在尝试使用 node.js 服务器和 Swift 2.2(基于 Frank van Puffelen 的 Sending notifications between Android devices with Firebase Database and Cloud Messaging)在各个设备之间发送推送通知。

通知请求已由 Firebase 数据库和 Node.js 服务器成功处理(向数据库添加请求、从数据库获取数据、向主题发送通知)但我没有收到任何警报 在我的设备上。

当我启动应用程序时,func application(application: UIApplication, didReceiveRemoteNotification) 被调用并且我能够打印通知,但是,与通过 Firebase 的界面发送通知相比,不幸的是没有警报.

userInfo 来自 Node.js 通知(No Alert):

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

userInfo 通过 Firebase 的界面发送通知(Alert works):

[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##]

索引.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();

如果有人能帮我解决这个问题,我将不胜感激:)

最佳答案

找到答案:我必须通过将 notification 中的 title 替换为 body 来更改 sendNotificationToUser 函数对象并将 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();
      }
    });
}

关于node.js - 使用 Node.js 服务器的 Firebase 云消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900210/

相关文章:

ios - Sprite Kit - SKSpriteNode 绘制在错误的位置

firebase - 在 Flutter 中从 Firebase 加载图像时使用 CircularProgressIndicator 小部件

javascript - 对象中的NodeJs FindbyId

node.js - 如何在 Windows 上的 npm 脚本中使用反引号

javascript - 在 javascript 中更新 DynamoDB 中包含连字符或破折号 (-) 的属性

javascript - 将 JSON 节点名称设置为变量值

http - Callable Cloud Functions 是否优于 HTTP 函数?

node.js - 更改更新不起作用 Angular-CLI (ng-serve)

ios - 我如何使用 AVCaptureSession 和 swift (IOS 8)在 mpeg4 容器中录制 mp4 视频

ios - 删除所有存在的节点