I\'m sending Firebase notifications through my own webservice with PHP like below.
$url = \'https://fcm.googleapis.com/fcm/send\';
$fields = array(
1.Notifications are coming when App is in Foreground and Background without any issues , But if I removed app from recent tray then notifications not coming , I must have to handle this any solution for this ? (like Whatsup notifications are showing all scenarios even I force stopped this app from settings)
As per your code you must be getting a null pointer exception on remoteMessage.getNotification().getBody()
as you are not sending notification, instead you are sending data payload from your curl request. As per my personal experience, the issue you just mentioned is seen especially in Android Kitkat or below devices. On Lollipop or above Firebase push seems to work fine.
2.Once I received notification , from "onMessageReceived" how to pass these message,body content to my Activity /Fragments?
You can issue a Broadcast from onMessageReceived()
and register a Braodcast Receiver in your Activity and send the data in your broadcast intent. Example code:
private BroadcastReceiver receiver;
@Overrride
public void onCreate(Bundle savedInstanceState){
// your oncreate code
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
}
};
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null
}
super.onDestroy();
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());
Intent intent=new Intent();
intent.setAction("SOME_ACTION");
intent.putExtra("message", remoteMessage.getNotification().getBody());
sendBroadcast(intent);
}
}
3.In some cases I want to send notification to all , Instead of adding all tokens to array . any other way to handle like "send to ALL" something like that ?
There is nothing like "Send to ALL" in Firebase API, the only way to do this is using Firebase Console which have its own issues to handle like white icon issue, and ripping the custom params of your notification.
I was having problem with FCM on exactly Mi Phone and Huawei phone as well! Turns out that it was not a problem with FCM or your app, but on their device level.
They have device level app settings that disable notifications or even wake lock, so onMessageReceived() is not fired when the message arrives. After I changed the settings (mainly allow notifications & disable battery optimisation for my app), everything works just fine!
Refer to Firebase cloud messaging not received when app in background.
This problem needs back-end implementation. Let me discuss how I overcome this shortcoming.
We are aware of the Activity life-cycle. So upon destroy, I call a service from the back-end that unregisters my client token. All incoming messages will now be queued for delivery since the token for target client is unregistered.
So upon bringing back the application, I call a back-end service that registers my client. The back end now starts to sends messages from queue with the new client token for my device.