Search code examples
javaandroidfirebase-notifications

Implement intent method after clicking the notification (NotificationCompat.Builder)


I'm developing a notification method. it's working successfully. but how can I implement Onclick method to navigate a specific activity?

private void notification() {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        {
            NotificationChannel channel=new NotificationChannel("n","n", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager=getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
                .setContentText("Alert")
                .setSmallIcon(R.drawable.ic_notifications)
                .setAutoCancel(true)
                .setContentText("Check your fit Room Request");

        NotificationManagerCompat managerCompat=NotificationManagerCompat.from(this);
        managerCompat.notify(999,builder.build());

    }

Solution

  •  Intent intent = new Intent(this, YourActivity.class);
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    Then

     NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
     .setContentIntent(pendingIntent)
     .....