0

Need help in the firebase notifications.

In my app I'm using FCM for sending push notifications to my app users.

Using Postman i'm sending data notifications to my app. After looking around on the net, i've done Data Notifications handling in my app and now I can show different data or open different activities based on parameters sent through notification.

But when i'm trying to do the same using firebase console with data notifications, its simply opening the app through luncher activity.

Something weird is going on here but i can't figure out.

With POSTMAN: Sending notification using Postman when app is in foreground triggers the targetActivity BUT when I try doing to same with app in background, i get the notification but clicking on notification simply go away from tray and do nothing.

With Firebase Console Sending notification through FCM Concole simply opens launcher activity and not the targetActivity. It doesn't matter if the app is in foreground or background.

Here is my code

POSTMAN REQUEST:

{
"to" : "cwDBStUSeB-MY-Firebase-Token-JOLscpe_6OWQoWnounUhw_trlY5Qw8w",

 "notification" : {
        "click_action" : ".SurveySlider", // i'm not using this 
        "body" : "Complete the survey and get bonus points.", 
        "title" : "Survey", 
         }, 
 "data": {  "transactionID" : "123456",
    "openActivity" : "Surveys",
    "message" : "This is message from data set."

 }
}

onMessageRecieved in my Android App

 {
        Intent intent = new Intent(this, BaseDrawerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        intent.putExtra("transactionID", remoteMessage.getData().get("transactionID"));
        intent.putExtra("targetActivity", remoteMessage.getData().get("openActivity"));

//        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
//            manager.createNotificationChannel(channel);
//        }

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(BaseDrawerActivity.class);

        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
       // builder.setContentIntent(resultPendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);



        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_logo_circle)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        int id = new AtomicInteger(0).incrementAndGet();
        manager.notify(id, builder.build());
}

BaseDrawerActivity to Handle Different target Activities based on notification data.

       Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            if (bundle.containsKey("targetActivity")) {
                String target = bundle.getString("targetActivity");    
                if (target.equalsIgnoreCase("Transactions")) {

                    MyActivity _myActivity = new MyActivity();
                    Bundle args = new Bundle();
                    args.putString("transactiontype", "all");
                    _myActivity.setArguments(args);
                    _fragmentTransaction = getSupportFragmentManager().beginTransaction().replace(R.id.content_base_drawer, _myActivity);
                    current = R.id.nav_Activity;
                    _fragmentTransaction.commit();


                } else if (target.equalsIgnoreCase("Surveys")) {
                    if (bundle.containsKey("transactionID")) {
                        String TID = bundle.getString("transactionID");
                        startActivity(new Intent(BaseDrawerActivity.this, SurveySlider.class).putExtra("Transaction_ID", TID));
                    } else {
                        Toast.makeText(getApplicationContext(), getText(R.string.transactionIDmissing), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    navigationView.getMenu().performIdentifierAction(R.id.nav_OverView, 0);
                    current = R.id.nav_OverView;
                    navigationView.getMenu().getItem(0).setChecked(true);
                }

            }else{

                    navigationView.getMenu().performIdentifierAction(R.id.nav_OverView, 0);
                current = R.id.nav_OverView;
                navigationView.getMenu().getItem(0).setChecked(true);

            }

        }
        else
            Log.d(TAG, "Bundle is NULL");
}

Please help me.

PS My app is based on Navigationdrawer with fragments. So if some can help me on handling the TargetActivites(Fragments) efficiently based on whats the target in Notification payload, please do so.

joe
  • 1
  • Did you read the documentation? https://firebase.google.com/docs/cloud-messaging/android/receive If your app is in background, the notification will be automatically appear in your system tray and leads to your launcher activity. – Prexx Jul 05 '18 at 14:33
  • @Prexx yes i've read it. but how I can change this behavior as it will always open launcher activity. and what about when the app is in foreground? still its opening launcher activity. – joe Jul 05 '18 at 14:35

0 Answers0