简体   繁体   English

GCM到FCM-在哪里添加服务器密钥和处理MessageException

[英]GCM to FCM - Where to add Server Key and handling MessageException

I'm doing migration according to deprecated of GCM from back end side. 我根据后端不推荐使用的GCM进行迁移。 Adding Admin SDK 添加管理SDK

dependencies {
  implementation 'com.google.firebase:firebase-admin:5.11.0'
}

After that, I do send message as follow 之后,我确实按照以下方式发送消息

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
String response = FirebaseMessaging.getInstance().sendAsync(message);

There are 2 questions in here: 这里有两个问题:

1. How do we set the key here? 1.我们如何在此处设置密钥? in GCM, we can do by new Sender(GCM_key).send(Message) but I can't find how to set key here. 在GCM中,我们可以通过new Sender(GCM_key).send(Message)但是我在这里找不到如何设置密钥的方法。 I don't think method setToken is used for setting key. 我不认为setToken方法用于设置密钥。

2. How do we handle exception here? 2.我们在这里如何处理异常? In the library, there's a class named FirebaseMessageException extend Exception, so how to handle it? 库中有一个名为FirebaseMessageException的类,它具有扩展异常,那么如何处理呢?

Any solution will be appreciated. 任何解决方案将不胜感激。

Thank you 谢谢

  1. When using the Admin SDK to send FCM messages, you don't need to set the Server Key anywhere in the request, the Admin SDK as it's named, has an Administrator-like privileges when it comes to the Firebase Services, so messages sent using it is automatically authorized. 使用Admin SDK发送FCM消息时,您无需在请求中的任何位置设置服务器密钥,因为Admin SDK的名称与Firebase Services 一样具有类似管理员的特权,因此使用它是自动授权的。

  2. As stated in the sendAsync() docs: sendAsync()文档所述:

Returns: An ApiFuture that will complete with a message ID string when the message has been sent. 返回:一个ApiFuture ,将在发送消息后以消息ID字符串完成。

From the returned ApiFuture , I presume you would just add a listener (ie addListener() ) or use the ApiFutures util class. 从返回的ApiFuture ,我假设您只是添加一个侦听器(即addListener() )或使用ApiFutures util类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM