0

I implemented push notification in Android using Firebase and I am receiving push notification using Firebase console. But when I try to connect to my server by sending device token and server key, then I am not receiving notifications (from my server).

Our server side team tested and it is working at their end, but my device is not receiving push notifications (device on which app is installed) and I am receiving push notification from Firebase.

How can I connect to the server? Is the problem in Android or with creating the google-services.json file?

AL.
  • 36,815
  • 10
  • 142
  • 281

1 Answers1

2

While Using Firebase push notification there are two cases to handle data payload( e.g. your Activity name etc.).

case 1: When your app is running in foreground(currently running on screen) you would get data payload from RemoteMessage object in onMessageReceived(RemoteMessage remoteMessage)method that you have overridden in your class that must extend FirebaseMessagingService class.

For example :

public class FirebaseMessagingServiceHelper extends FirebaseMessagingService {
    private final String TAG = "FirebaseMsgServiceHelper";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Map<String, String> dataPayload = null;
    dataPayload = remoteMessage.getData();
    }
}

case 2: When your app is running in background you would get data payload as extras in intent in your app's launcher Activity.

For Example :-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //get notification data info
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
    //bundle must contain all info sent in "data" field of the notification
   }
}

But these two cases were not working properly when I got name of the Activity that is supposed to be open when user clicks on a notification on Notification Drawer in click_action and data payload in data.

But when I putted down everything in data it worked fine for me in both the cases i.e. whether app is running in foreground or in background your app would receive data payload everytime in RemoteMessage object in onMessageReceived(RemoteMessage remoteMessage) method.

Below is the link I link from where I got idea. Firebase FCM notifications click_action payload

Community
  • 1
  • 1