0

I have PushNotificationService and my method onMessageReceived.

If I am in the program, a notification comes from above and directs me to the page I want.

However, when the program is closed, when I click on the notification in the background, it does not go to the page I want, it only goes to the homepage.

And I want to open activity when ı click to notification. How can I do ?

public class PushNotificationService extends FirebaseMessagingService {

    private NotificationCompat.Builder builder;

    @SuppressLint("NewApi")
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {

        String baslik = message.getNotification().getTitle();
        String icerik = message.getNotification().getBody();
        durumaBagli(baslik,icerik);
        
    }
    private void durumaBagli(String baslik, String icerik) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this,BildirimGonder.class);

         PendingIntent pendingIntent = PendingIntent
                .getActivity(this,1,intent,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

            String kanalid = "kanalId";
            String kanalAd = "kanalAd";
            String kanalTanim = "kanalTanim";

            int kanalOnceligi = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel kanal = notificationManager.getNotificationChannel(kanalid);

            if (kanal == null){
                kanal = new NotificationChannel(kanalid,kanalAd,kanalOnceligi);
                kanal.setDescription(kanalTanim);
                notificationManager.createNotificationChannel(kanal);
            }

            builder = new NotificationCompat.Builder(this,kanalid);
            builder.setContentTitle(baslik);
            builder.setContentText(icerik);
            builder.setSmallIcon(R.drawable.gurpinarlogom);
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
        }else {
            builder = new NotificationCompat.Builder(getApplicationContext());
            builder.setContentTitle(baslik);
            builder.setContentText(icerik);
            builder.setSmallIcon(R.drawable.gurpinarlogom);
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
            builder.setPriority(Notification.PRIORITY_HIGH);
        }

        notificationManager.notify(1,builder.build());
    }
}

And my MainActivity

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNav;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                     
        bottomNav = findViewById(R.id.bottomNav);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(bottomNav, navHostFragment.getNavController());
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#01A2D6")));
    }
    public void setActionBarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_iletisim:
                Intent mesajGonder = new Intent(getApplicationContext(), MesajGonder.class);
                startActivity(mesajGonder);
                Toast.makeText(this, "İletişim sayfası", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

This my codes

yAnTar
  • 4,269
  • 9
  • 47
  • 73
Revenge
  • 3
  • 3

2 Answers2

0

Put this Flag to your intent like that:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Stam
  • 2,410
  • 6
  • 32
  • 58
0

According to this post maybe your FCM type of message does not trigger the onMessageReceived() function when the app is in foreground/background/killed.

Make sure that the FCM message type is Data Messages

Checkout the post as it contain detail about how to setup the FCM correctly so that the notification trigger the onMessageReceived() when the application is in foreground/background/killed

Khanh Tran
  • 56
  • 6