How to start the desired activity when clicking on notifications (Notification) even if the application is not running?

Here is my display notification:

private void sendNotification(String url_post, String thumb_post, String message_title, String message_body) { Intent intent = new Intent(this, SinglePostActivity.class); intent.putExtra("_post_url", url_post); intent.putExtra("_post_title", message_body); intent.putExtra("_toolbar_title", message_body); intent.putExtra("_toolbar_thumb", thumb_post); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.beloweb_icon_small) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle(message_title) .setContentText(message_body) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis()) .setDefaults(Notification.DEFAULT_SOUND) .setVibrate(new long[]{100, 600, 500, 600}) .setLights(Color.GREEN, 400, 400) .setTicker(message_body); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notificationBuilder.build()); } 
  • Your problem is not clear. What exactly you can not? You run SinglePostActivity, and you want a different activity and cannot replace SinglePostActivity with the name of another class? - Yuriy SPb
  • No, I click on notifications and instead of SinglePostActivity, MainActivity is launched. And when the application is running and notifications come, SinglePostActivity opens. I need to do so. For example, the application is installed but not open. Notifications come here and when you click, the applications should open and SinglePostActivity will start immediately and not MainActivity - Alexander
  • Look here - maybe you need to configure which actviti to open in FireBase, and also configure intentFilter in the manifest - YuriySPb

1 answer 1

Judging by the en-SO, you need to specify in FireBase, when creating a click_action notification with a -l value, something must be identical to the IntentFilter target IntentFilter

 { "registration_ids": [ "XXX", ... ], "data": { "id_offer": "41" }, "notification": { "title": "This is the Title", "text": "Hello I'm a notification", "icon": "ic_push", "click_action": "ACTIVITY_XPTO" } } 

In the manifest we specify it in the filter

 <activity android:name=".ActivityXPTO" android:screenOrientation="sensor" android:windowSoftInputMode="stateHidden"> <intent-filter> <action android:name="ACTIVITY_XPTO" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>