There is a ton of documentation explaining how to send messages from the server to the Android device, but next to nothing available for how to send it from the Android device to the server.
We are working on making an app where the users can receive notifications from one another in certain circumstances. I am receiving messages just fine and my code works to parse them when received. Sending messages, on the other hand, is not working.
The code I am attempting to work with is:
public static void sendNotificationToUser(String userId, final String message){
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
Bundle someBundle = new Bundle();
someBundle.putString("STRING_KEY_HERE", "STRING_VALUE_HERE");
//I have tried excluding the @gcm as well, no luck.
String str = "user_" + userId + "@gcm.googleapis.com";
RemoteMessage.Builder myBuilder = new RemoteMessage.Builder(str);
Map<String, String> aMap = new HashMap<>();
aMap.put("DATA_KEY_1", "DATA_VALUE_1");
myBuilder.setData(aMap);
myBuilder.addData("ADD_DATA1", "ADD_DATA11");
myBuilder.setMessageId("SOME_MESSAGE_ID123");
myBuilder.setTtl(0); //Send immediately
myBuilder.setMessageType("SOME_MESSAGE_TYPE");
myBuilder.setCollapseKey("SOME_COLLAPSE_KEY");
//Can't do this, private access, how do I instantiate this?
RemoteMessage.Notification notification = new RemoteMessage.Notification();
RemoteMessage remoteMessage = myBuilder.build();
messaging.send(remoteMessage);
}
The problem here is that once this fires off, nothing ever happens. Specifically, what I am trying to do is send a message as per subscribed topics. So, if I were subscribed to android_tag
, I want to send this message to the android_tag
, which is where the userId
is set; I have that user subscribed to user_{theirtag}
.
Does anyone have any clue how to go about sending an outbound FCM message to a specific topic? Also, how do I instantiate and include a RemoteMessage.Notification
object since it cannot be done the way I have listed above?
Thanks all.
EDIT: It looks like the answer as per Mr Frank van Puffelen is that no, it cannot be done via the SDK directly. It can be done via a combination of topic subscriptions and including a server to process the actual sends (here), but it should not be done without the use of a server in order to prevent secret/ server key exposure.