[英]How to push notification with Cloud Messaging Firebase from the server
I already have an app and I want to start sending notification to the users. 我已经有一个应用程序,我想开始向用户发送通知。 I already set up everything in the app(using react native) and I checked manually that I can send notification to the devices and it works.
我已经在应用程序中设置了所有内容(使用react native),并手动检查可以将通知发送到设备,并且它可以正常工作。 Now I want to run a job in the server who will push the message (with the device token) to the cloud messaging in firebase.
现在,我想在服务器中运行一个作业,该作业会将消息(带有设备令牌)推送到firebase中的云消息传递中。 I can't find a lot of details about how to do it.
我找不到有关如何执行此操作的许多详细信息。 I would like if someone can give me any guide I can use with.
我希望有人可以给我提供任何指导。 my server is in Kotlin(java can be good too) and I m working with gradle.
我的服务器在Kotlin(java也可以),并且我正在使用gradle。
Thank you so much for the help 非常感谢你的帮助
From a Java server you can use the Firebase Admin SDK to send messages . 您可以从Java服务器使用Firebase Admin SDK发送消息 。 From that documentation comes this minimal example:
该文档提供了以下示例:
// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setToken(registrationToken)
.build();
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Note that this sends a data message, so that will always be delivered to your code , where you can decide to display a notification or not. 请注意,这会发送一条数据消息,因此它将始终传递到您的代码中 ,您可以在其中决定是否显示通知。 To send a notification message, which is what the Firebase console does, you'd use:
要发送通知消息(这是Firebase控制台的工作),请使用:
Message message = Message.builder()
.setNotification(new Notification("This is the title", "This is the body"))
.setToken(registrationToken)
.build();
Both of these send the message to a specific registration token, so only to a single device/app instance. 两者都将消息发送到特定的注册令牌,因此仅发送到单个设备/应用程序实例。 This means you will need to maintain a list of these tokens, in a way that allows you to send the messages to fit your needs.
这意味着您将需要维护这些令牌的列表,以使您能够发送满足其需求的消息。 Eg a common way is to store the tokens per user.
例如,一种常见的方法是为每个用户存储令牌。 For an example of that, see the functions-samples repo .
有关示例,请参见functions-samples repo 。 While this example is in Node.js, the same logic could be applied to a Java server.
尽管此示例位于Node.js中,但相同的逻辑可以应用于Java服务器。
Finally: you can also send message to topics. 最后:您还可以向主题发送消息。 For an example of that (again: using a Node.js server), have a look at this blog post Sending notifications between Android devices with Firebase Database and Cloud Messaging .
有关该示例的示例(再次:使用Node.js服务器),请参阅此博客文章, 在具有Firebase数据库和Cloud Messaging的Android设备之间发送通知 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.