Firebase onMessageReceived() returns no getNotification

177 Views Asked by At

Calling remoteMessage.getNotification() returns null and when calling remoteMessage.getData() I get a strange object back that has an initial property _wp={ as listed below. I need to extract the alert property each time but I am not sure how.

public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    Map<String, String> data = remoteMessage.getData();

    Log.d("DATA", remoteMessage.getData().toString());
    sendNotification(data);
}

The remote data log returns the below. So I can't seem get the title and text I need to in order to construct my notification.

{_wp={"c":"01euntvtna3epk83","alert":{"text":"Body text","title":"test"},"receipt":false,"type":"simple","targetUrl":"wonderpush:\/\/notificationOpen\/default","n":"01eunu08bjla8303","reporting":{"campaignId":"01euntvtna3epk83","notificationId":"01eunu08bjla8303"},"receiptUsingMeasurements":true}, alert=Body text}

I basically want to use them with when using the NotificationCompat.Builder

.setContentTitle(title)
.setContentText(text)

Any help will be greatly appreciated.

1

There are 1 best solutions below

1
ofavre On BEST ANSWER

It is normal that remoteMessage.getNotification() returns null as WonderPush only uses FCM notifications with data inside them, nothing in the Firebase's own format.

remoteMessage.getData() returns you with a map that can only store String values. This corresponds to the top-level JSON payload. This map's fields are the top-level JSON object fields, and its values are all stringified. So you'll have to parse the _wp key using new JSONObject(remoteMessage.getData().get("_wp")). You'll basically read the title and text fields of this parsed _wp JSON object to feed .setContentTitle() and .setContentText() of the NotificationCompat.Builder.

But you should note that the WonderPush Android SDK is precisely here for that purpose:

If you have an addition, it would make more sense to fork, hack, submit a pull request, and use your fork in the meanwhile.

Best,