在Firebase控制台中,我看到了通过应用程序“com.example”(其中com.example是应用程序名称)向User Segement发送通知的选项。从服务器向安装应用程序的用户段发送通知
正如图所示:
但如何从服务器端使用FCM REST API做到这一点:
https://fcm.googleapis.com/fcm/send
在Firebase控制台中,我看到了通过应用程序“com.example”(其中com.example是应用程序名称)向User Segement发送通知的选项。从服务器向安装应用程序的用户段发送通知
正如图所示:
但如何从服务器端使用FCM REST API做到这一点:
https://fcm.googleapis.com/fcm/send
实际上,你需要发送一个消息话题..所有订阅了主题的会员都会收到你的消息。
只需退房链接..
https://developers.google.com/cloud-messaging/topic-messaging
发表的帖子调用https://fcm.googleapis.com/fcm/send具有以下参数: -
头: -
内容类型 - 应用程序/ JSON
Authorization-- key = {你的服务器密钥}
正文: -
{
"data": {
"my_custom_key" : "my_custom_value",
"message" : "notification message"
},
"registration_ids": ["device_token1,device_token2,.........."]
}
编辑: -
基本上你需要做的是,每当ü需要发送通知,你必须从你的服务器端和您的应用程序调用此POST方法将自动获得在OnMessageReceived通话。你可以这样处理: -
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Map<String, String> data=remoteMessage.getData();
Log.d(TAG, "From: " + data.toString());
String value=data.get("my_custom_key");
Log.d(TAG, "From: " + value);
String msg=data.get("message");
Log.d(TAG, "From: " + msg);
sendNotification(msg,value,remoteMessage.getSentTime());
}
你将不得不记住一些事情。 首先,你必须附上标题:
Content-Type--application/json
Authorization--key={your server key with key= in the value.Don't miss key= here}
接下来的事情是身体:
{
"data": {
"my_custom_key" : "chat",
"message" : "chat msg",
"priority":"high"
},
"registration_ids": ["ids of the users"]
}
这是你request.Their的身体有两种类型:数据和notification.You可以使用他们中的任何一个或两者。你可以理解这种差异,从这个link。基本上,如果您发送数据,您将不得不在前台和后台阶段处理通知。在通知类型中,后台通知将通过通知托盘进行处理。您可以查看链接。
然后你有一个json字符串数组,你将不得不发送所有你想发送通知的用户标记。你可以添加更多的功能,你可以在firebase文档中看到这是基本的实现为你。希望这可以帮助
''registration_ids“ :[“用户的ids”]':我如何发送给安装应用程序的用户段而不是id。参考更新的问题。如何使用服务器端实现与图像中显示的相同的东西url –
检查这个http://stackoverflow.com/questions/37995870/how-to-send-firebase-notifications-to-audience-via-http.I不要不认为他们是一种方式。 –
所以没办法做到这一点 –
不幸的是,它是not possible to send messages to User Segments using the FCM REST API。
作为一种替代方案,您必须使用send messages to multiple devices的其他方式,如使用registration_ids
参数和主题消息(我认为这是您的最佳用例)。
以下是关于如何发送此using Postman或cURL的示例。
我不能要求我的会员订阅主题。谁安装应用程序将开始得到通知 - –
我知道了@SanthoshYedidi - 你应该继续前进,并使用'registration_ids'。如果你有超过一千个用户,你将不得不提出批量请求,每个请求最多有1000个注册令牌。 –
订阅用户根据不同的操作系统
话题上: “机器人” 为Android用户
话题: “iOS版” 适用于iOS用户
(或任何你想要的名称)
然后发送到该主题...
我找到了一个解决方案 你可以订阅你的应用程序到一个特定的主题,例如你在FirebaseInstanceIdService R级的应用程序包名称,这样你们可以发送数据按摩像
{
"to" : "/topics/your_package_name",
"data" : {
"key1" : "value 1",
"key2": "value 2",
...
}
}
这里是订阅您的应用程序,以主题FirebaseInstanceIdService类
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService
{
private final String TAG="your_tag";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
FirebaseMessaging.getInstance().subscribeToTopic("your_app_package_name");
}
}
它的工作对我来说
我可以获得数据[按摩](https://en.wikipedia.org/wiki/Massage)吗?听起来不错。 – 0xbaadf00d
是的,我认为是。这是非常好的和棘手的解决方案 –
我的代码不能要求我的会员订阅主题。安装应用程序的人将开始收到通知 –