I ran into the same problem and after spending some time trying to figure out the reason, my observation was -
Since the notification field is a JSON representation of RemoteMessage.Notification . If you set any of the predefined fields of the Notification class to the "notification" field, on the client side the JSON is parsed successfully and you have a non-zero value for RemoteMessage.getNotification() , on which you can call getBody() / getTopic() / getIcon() .
But if you do not set any field of the Notification class to the json notification field, the parsing in the class will fail and you will have a zero value for RemoteMessage.getNotification()
So, any of the following three JSONs is the actual POST body for pressing RemoteMessage.Notification (in addition to the two examples shared by Andrea in the earlier answer), i.e. these three will not cause the aforementioned NPE
{ "to" : "<<FIREBASE_INSTANCE_ID>>", "notification" : { "body" : "Notification Message Body" } } { "to" : "<<FIREBASE_INSTANCE_ID>>", "notification" : { "title" : "Notification Title" } } { "to" : "<<FIREBASE_INSTANCE_ID>>", "notification" : { "icon" : "Notification icon" } }
And none of the following three is valid for pushing RemoteMessage.Notification -
Does not have a notification field
{ "to" : "<<FIREBASE_INSTANCE_ID>>" }
Notification field is empty json
{ "to" : "<<FIREBASE_INSTANCE_ID>>", "notification" : { } }
There are several key value pairs in the notification field, but none of the fields defined in RemoteMessage.Notification class
{ "to" : "<<FIREBASE_INSTANCE_ID>>", "notification" : { "messageText" : "Notification Message Text", "messageBody" : "Notification Message Body" } }
source share