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