use*_*869 6 java rest android appserver firebase-cloud-messaging
我是新手,可以通过https://firebase.google.com/docs/cloud-messaging/server在Android应用中使用FCM通知.我正在阅读它并发现在关于FCM Server页面要求中,它说明如下:
您必须在您的环境中实施的应用服务器.此应用服务器使用适当的XMPP或HTTP协议通过所选的FCM连接服务器将数据发送到客户端应用程序
但是,我对此感到非常困惑.当我在文章中阅读更多内容时,我发现有一个API看起来像这样:
POST http://fcm.googleapis.com/fcm/send
如果我使用类似的东西调用此API OkHttpClient并构建我的请求,(假设我有身份验证标头和POST主体)
private void sendRegistrationToServer(String token) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder().add(“Body", "").build();
//Assuming I have authentication and body put in
Request request = new Request.Builder().url("http://fcm.googleapis.com/fcm/send”).post(body).build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
理论上,我是否能够通过我想要的任何信息向该设备发送通知?我可以通过以下课程收到消息:
public class NotificationService extends FirebaseMessagingService {
...
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
Run Code Online (Sandbox Code Playgroud)
我确信我的理解是不正确的,因为文档确实说我们需要一个应用程序服务器,但如果有人可以指出我误解了如何实现FCM通知的地方,那就太好了.如果有人可以举例说明我们何时何地需要app服务器,或者它应该如何实现,那也是非常值得赞赏的.谢谢!
小智 1
当文档说您需要应用程序服务器时,主要是因为您需要一个应用程序来存储您想要向其发送通知的设备的令牌,并且如果您的任何客户端设备更改其令牌,则该应用程序应该更新令牌。但是,您可以使用OkHttpClient向 FCM 服务发送请求,从而向其他设备发送通知(当然,如果您拥有这些设备的令牌 ID)。这取决于您想要执行的操作以及您想要如何管理通知。
如果您想要一个关于如何在 java 中实现服务器应用程序的示例,这里是一个很好的示例示例 1,它已发布,或者这里是另一篇关于 PHP 实现的帖子。如果您想要有关如何实现客户端应用程序以及如何从 firebase 控制台测试它的示例,这里是另一个很好的示例。
| 归档时间: |
|
| 查看次数: |
4752 次 |
| 最近记录: |