简体   繁体   中英

GCM to FCM - Where to add Server Key and handling MessageException

I'm doing migration according to deprecated of GCM from back end side. Adding Admin 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? in GCM, we can do by new Sender(GCM_key).send(Message) but I can't find how to set key here. I don't think method setToken is used for setting key.

2. How do we handle exception here? In the library, there's a class named FirebaseMessageException extend Exception, so how to handle it?

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.

  2. As stated in the sendAsync() docs:

Returns: An ApiFuture that will complete with a message ID string when the message has been sent.

From the returned ApiFuture , I presume you would just add a listener (ie addListener() ) or use the ApiFutures util class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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