1

I am working on an android app with fcm notification. I handle the notification and I put intent with specific classes for each notification type. but when I click on the notification is leads me to the main activity (which I not want it and not put it in the intent) I don't know what I missed!!

This is my code:

    private void sendNotification(String messageBody, String title) {
        Intent intent;
        EventObject eventObject = new EventObject(0,"","");


        if(type.equals("order_pending") ){
            intent = new Intent(this, orderDetailsActivity.class)
                    .putExtra("orderId", order_id);
            eventObject = new EventObject(Integer.parseInt(order_id),"order_pending", messageBody);
        }

        else if( type.equals("order_declined")) {
            intent = new Intent(this, orderDetailsActivity.class)
                    .putExtra("orderId", order_id);
            eventObject = new EventObject(Integer.parseInt(order_id) ,"order_declined", messageBody);

        }

        else {
            intent = new Intent(this, MainActivity.class);
            eventObject = new EventObject(1,"Activate Agent",messageBody);
        }


        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        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.drawable.logo)
                        .setContentTitle(title)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        int f =0;
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        EventBus.getDefault().post(eventObject);


            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
     }
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
Haya Akkad
  • 281
  • 4
  • 15
  • 1
    it's always entered on `if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isActive", true)) ` that's why, check put log before and after, which will give you cause of it – ॐ Rakesh Kumar Mar 18 '20 at 10:12
  • thank u for ur answar, but even I removed this if close, the issue still!, I will update my code question. – Haya Akkad Mar 18 '20 at 10:19
  • 1
    still, you have `type` conditions, I recommend you to use log on these conditions, also check if code block gets reached – ॐ Rakesh Kumar Mar 18 '20 at 11:09
  • I check no and I saw that the notification received from the firebase with out enter to my method (send notification) because the fcm sending with Notification type not with data type.. so in this status, how I can link the received notification with spesific Activity when the app in background? – Haya Akkad Mar 18 '20 at 13:21
  • 1
    then you can't do anything, for that you will be needed to have data notification, which works on background/foreground and on kill state – ॐ Rakesh Kumar Mar 18 '20 at 13:23
  • i saw this: click_action: "com.example.myapplication_YOUR_NOTIFICATION_NAME" should set from the firebase.. so i wana ask that the "click_action" value should be the activity name? if yes, like that: actvityName.class? – Haya Akkad Mar 18 '20 at 13:24
  • I can't set the activity name from the firebase? – Haya Akkad Mar 18 '20 at 13:29
  • 1
    thank you very much @RakeshKumar I get the value and handel the notification action using this selotions: https://stackoverflow.com/a/40185654/9920144 – Haya Akkad Mar 18 '20 at 14:42

0 Answers0