0

When app is not running and user clicks on notification it will open splash screen but it getExtras there is no key like title and body. But if app is working and notification sending from MyFirebaseMessagingService then it will print title and body. What is the issue here?

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        String value = getIntent().getExtras().getString(key);
        switch (key){
            case "body":{
                if(value.contains("order successfully")){
                    orderSuccessfully=true;
                }
                break;
            }
        }
        Log.d("readingNotiMain", "Key: " + key + " Value: " + value+"  vc:"+value.contains("order successfully"));
    }
}



public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
/*        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            if (*//* Check if data needs to be processed by long running job *//* true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }
        }*/

       // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());

            if(remoteMessage.getData()!=null){

                Log.i(TAG,"data:"+new Gson().toJson(remoteMessage.getData())+"");
                Log.i(TAG,"don "+new Gson().toJson(remoteMessage.getData()));
            }

            Log.d(TAG, "Message Notification title: " + remoteMessage.getNotification().getTitle()
                    +" \n Body"+ remoteMessage.getNotification().getBody());

        }
    }

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
        sendRegistrationToServer(token);
    }
    // [END on_new_token]

    private void scheduleJob() {
//        // [START dispatch_job]
//        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
//        Job myJob = dispatcher.newJobBuilder()
//                .setService(MyJobService.class)
//                .setTag("my-job-tag")
//                .build();
//        dispatcher.schedule(myJob);
//        // [END dispatch_job]
    }


    private void handleNow() {
        Log.d(TAG, "Short lived task is done.");
    }


    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }


    private void sendNotification(String title,String messageBody) {
        Intent intent = new Intent(this, Main_Act.class);
        intent.putExtra("body",messageBody);
        intent.putExtra("title",title);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Intent orderPageIntent=new Intent(getApplicationContext(),Main_Act.class);
        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "ssss",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
Midhilaj
  • 4,905
  • 9
  • 45
  • 88

1 Answers1

0

Actually, in FCM there is tow type of payload(Message types) available, one is Data and another one is Notification, so If you are using notification payload then when the app is in background you cant handle notification in onReceive and in foreground you can handle it, so I just suggest if you want to handle data in both case use then put only data payload don't put notification payload

You can reach more understanding from bellow document

About FCM messages Types

Here is the server-side payload example

With Notification Payload

{"to":"[add your token]","notification":{"title":"Working Good","body":"[add your message]"},"priority":"high"}

With Data Payload

{"to":"[add your token]","data":{"title":"Working Good","body":"[add your message]"},"priority":"high"}
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39