我已经使用Firebase创建了推送通知服务,我可以向拥有FCM ID的所有用户或单个用户发送通知,但我不知道如何发送给特定用户。
此外,还没有为处理推送通知处理而创建服务器面板如果对此提出任何建议会有更多帮助。
答案 0 :(得分:2)
Firebase云消息传递(FCM)主题消息传递允许您向已选择加入特定主题的多个设备发送消息。基于发布/订阅模型,主题消息支持每个应用程序的无限订阅,即您的组附加到特定主题,如新闻组,体育组等。
FirebaseMessaging.getInstance().subscribeToTopic("news");
取消订阅unsubscribeFromTopic("news")
从服务器端,您需要设置特定主题,即一组这样的用户:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/news",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
"/topics/news"
这会向通知新闻主题的人群发送通知
答案 1 :(得分:1)
在你的Android代码中:
public static void sendNotificationToUser(String user, final String message) {
Firebase ref = new Firebase(FIREBASE_URL);
final Firebase notifications = ref.child("notificationRequests");
Map notification = new HashMap<>();
notification.put("us
ername", user);
notification.put("message", message);
notifications.push().setValue(notification);
}
创建一个节点并将此代码放入:
var firebase = require('firebase');
var request = require('request');
var API_KEY = "..."; // Your Firebase Cloud Server API key
firebase.initializeApp({
serviceAccount: ".json",
databaseURL: "https://.firebaseio.com/"
});
ref = firebase.database().ref();
function listenForNotificationRequests() {
var requests = ref.child('notificationRequests');
ref.on('child_added', function(requestSnapshot) {
var request = requestSnapshot.val();
sendNotificationToUser(
request.username,
request.message,
function() {
request.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();
使用以下链接的更多信息,与许多设备相同:
Sending notification between android devices with Firebase Database and Cloud Messaging
答案 2 :(得分:1)
我没有足够的声誉来编辑Burhanuddin Rashid的答案,但我认为OP需要的是:
您可以将"to: /topics/news"
替换为registration_ids
{
"registration_ids" : [
"UserInstanceToken1",
"UserInstanceToken2"
]
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
可以通过
获取用户实例令牌 Android中的 FirebaseInstanceId.getInstance().getToken()
。
答案 3 :(得分:0)
享受!
public class NotificationSenderThread implements Runnable {
private String title;
private String message;
private String senderToken;
private String recieverToken;
public NotificationSenderThread(String title, String message, String senderToken, String recieverToken) {
this.title = title;
this.message = message;
this.senderToken = senderToken;
this.recieverToken = recieverToken;
}
@Override
public void run() {
try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("title", title);
jsonObject.put("message", message);
jsonObject.put("fcm_token", senderToken);
JSONObject mainObject = new JSONObject();
mainObject.put("to", recieverToken);
mainObject.put("data", jsonObject);
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "key=<SERVER KEY>");
connection.setDoOutput(true);
Log.e("sent",mainObject.toString());
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(mainObject.toString());
dStream.flush();
dStream.close();
String line;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder responseOutput = new StringBuilder();
while((line = bufferedReader.readLine()) != null ){
responseOutput.append(line);
}
bufferedReader.close();
Log.e("output", responseOutput.toString());
}
catch (Exception e){
Log.e("output", e.toString());
e.printStackTrace();
}
}
}