How to open the current activity that opens when you click on a notification

I tried all the methods, but this does not work for me. I want to open or resume the application by opening any screen by clicking on the notification.

I used the following method:

NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle(team); notiStyle.bigText(message); Intent resultIntent = new Intent(this, MainDrawerActivity.class); resultIntent.putExtra("fromNotification", "notification"); resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); int icon = R.mipmap.ic_launcher; return new NotificationCompat.Builder(this).setSmallIcon(icon) .setAutoCancel(true) .setContentIntent(resultPendingIntent).setContentTitle(team) .setContentText(message).setStyle(notiStyle).build(); 
+5
source share
6 answers

To bring the application to the forefront without starting any new actions, launch it.

This method is from my old project.

 /** * Creates a new launcher intent, equivalent to the intent generated by * clicking the icon on the home screen. * * @return the launcher intent */ public static Intent newLauncherIntent(final Context context) { final Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); return intent; } 

The target created by this method does not start a new task if the application is running, although it has this flag.

This is another way to get the launch intent. However, I found that this intention will always start a new task, which is not what you want if the application is running.

 final Intent intent = context.getPackageManager() .getLaunchIntentForPackage(BuildConfig.APPLICATION_ID); 
+4
source

This works fine under the following three conditions:

1. If the application is already open and click on the notification, the notification should be removed from the status bar.

2.if the application is open and in the background, then the application should resume from any previously open screen.

3. The application closes and clicks on the notification in the status bar, after which the application should open.

 private final static int NORMAL = 0x00; private final static int BIG_TEXT_STYLE = 0x01; private static NotificationManager mNotificationManager; 

in onMessage call

 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); new CreateNotification(BIG_TEXT_STYLE, team, message).execute(); 

then declare the next class in the GCMIntentService. Public class CreateNotification extends AsyncTask {

  int style = NORMAL; String team, message; public CreateNotification(int style, String team, String message) { this.style = style; this.team = team; this.message = message; } @Override protected Void doInBackground(Void... params) { Notification noti = new Notification(); switch (style) { case BIG_TEXT_STYLE: noti = setBigTextStyleNotification(team, message); break; } noti.sound = (null); noti.defaults = 0; noti.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.beep); noti.flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(0, noti); return null; } } 

and finally

 private Notification setBigTextStyleNotification(String team, String message) { // Create the style object with BigTextStyle subclass. NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle(team); notiStyle.bigText(message); Intent resultIntent = getPackageManager() .getLaunchIntentForPackage(getPackageName()); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the Intent that starts the Activity to the top of the stack. stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); int icon = R.mipmap.ic_launcher; return new NotificationCompat.Builder(this).setSmallIcon(icon) .setAutoCancel(true) .setContentIntent(resultPendingIntent).setContentTitle(team) .setContentText(message).setStyle(notiStyle).build(); } 
+1
source
 //I am using write now this can possible NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); Intent notificationIntent = new Intent(context, HomeActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); 
0
source

make new activity

 public class FinishImmediateActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); finish(); } } 

add to manifest.xml

 <activity android:name=".FinishImmediateActivity"/> 

check that the application is working

 public static boolean isMainActivityRunning() { ActivityManager activityManager = (ActivityManager) MyApp.getContext().getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(Integer.MAX_VALUE); for (int i = 0; i < tasksInfo.size(); i++) { if (tasksInfo.get(i).baseActivity.getPackageName().equals(MyApp.getContext().getPackageName())) { return true; } } return false; } 

then call this activity in the intent of notification.

 Intent resultIntent = new Intent(this, isMainActivityRunning() ? FinishImmediateActivity.class : HomeActivity.class); 
0
source

If you want to simply resume the state of the application, then instead of several actions, I suggest that you just keep a single activity and use fragments for another screen.

In the "Notification" window, you need to determine the entry point of the application in the notification payload, and the entry point - decide what will be the next navigation.

If you have only one activity, you can define this activity as an entry point and actions that you can decide whether you need to click a new fragment or not.

Or the second option, if you use firebase, and then click the entire notification as a background notification and onMessageReceive method, you can get the top activity from the action stack and set this action as the entry point for the notification. But there is still a problem, because the user can click on the notification after switching from the specified activity of the entry point, which is again a problem. Therefore, I prefer to use the first approach.

0
source

Thus, we can achieve the specified result:

  try { int icon; icon = R.mipmap.ic_launcher; int mNotificationId = 001; Intent intent = new Intent(this, MainDrawerActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //FLAG_UPDATE_CURRENT is important PendingIntent pendingIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this); Notification notification = mBuilder.setSmallIcon(icon).setTicker(json.getString("team")).setWhen(0) .setAutoCancel(true) .setContentTitle(json.getString("team")) .setStyle(new NotificationCompat.BigTextStyle().bigText(json.getString("message"))) .setContentIntent(pendingIntent) .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.beep)) NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(mNotificationId, notification); } catch (Exception e) { e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/1261541/


All Articles