With the new update, FCM is now going to be used.
I tried the sample app from git and it's working all fine. I can send notifications from the console.
But I want to send the notification from the server after a certain event is triggered. I followed the same approach like in GCM but it's not working.
05-20 20:40:58.941 30132-30919/com.google.firebase.quickstart.fcm E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
Process: com.google.firebase.quickstart.fcm, PID: 30132
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference
at com.google.firebase.quickstart.fcm.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:53)
at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source)
at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source)
at com.google.firebase.iid.zzb$2.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
05-20 20:40:59.118 30132-30279/com.google.firebase.quickstart.fcm E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9e83390
Am following this PHP Script to send the notification. If I try to execute the script, I get the following result.
{"multicast_id":4679427854122301046,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1463757518309261%31bd1c96f9fd7ecd"}]}
NOTE : I went through their docs and modified the code is gist to have only body and title. Even then it's not working.
I was facing the same issue and after spending some time trying to find out the cause , my observation was --
Since the "notification" field is JSON representation of the RemoteMessage.Notification . If you set any of the predefined fields of the Notification class in the "notification" field , on client side the JSON is successfully parsed and you have a non-null value for
RemoteMessage.getNotification()
on which you can call thegetBody()
/getTopic()
/getIcon()
.But if you don't set any field of the Notification class in the "notification" json field, the parsing to class fails and you will have a null value for
RemoteMessage.getNotification()
So, any of the following three JSONs is a valid POST body for pushing a
RemoteMessage.Notification
( in addition to the two examples shared by Andrea in earlier answer ) , i.e. these three won't cause the above NPEAnd none of the following three are valid for pushing a RemoteMessage.Notification -
Doesn't have the "notification" field
"notification" field is an empty json
"notification" field has some key value pairs , but none of the fields defined in RemoteMessage.Notification class
You can use this complete code
Pass message and token id as a parameter to the
sendFCM($mess,$id)
call.From the php gist you are sending a data only message. Your receiver is expecting a notification message so when you get the notification from the remote message it will be null, resulting in a NPE when you call getBody.
Send a notification message and it should work as expected. See notification message requirements here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
Try this below code this will give push notication for Android from php server side and you can get the device token from android you need pass dynamically to get push notication for more android device.
I tried this and worked:
This is the result:
In order to receive the notification using remoteMessage.getNotification().getBody(), you have to use the predefined set of key option for notification.
In this case, "notification" is the key word.
The JSON response has to be formatted like this.
You can also send notification and data payload in the same JSON response
see this: https://firebase.google.com/docs/cloud-messaging/concept-options#messages-with-both-notification-and-data-payloads