How to open current activity which is open while click on notification

只谈情不闲聊 提交于 2020-01-23 06:03:27

问题


I have try all the methods but it doesn't work for me. i want to open or resume app with whatever screen open while click on notification.

I used 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(); 

回答1:


To raise your application to the foreground without starting any new activity, fire its launcher intent.

This method is from an old project of mine.

/**
 * 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 intent created by this method does not start a new task if the app is running, even though it has that flag.

This is another way to obtain a launcher intent. However, I found that this intent would always start a new task, which is not what you want if the app is running.

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



回答2:


This is working fine for following three conditions:

1.if app already open and click on notification, notification should remove from status bar.

2.if app is open and in background then app should resume with whatever screen open already previously.

3.if app is close and click on notification in status bar then app 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 following class in 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();
}



回答3:


//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);



回答4:


If you want to just resume the app state then instead of multiple activity I will suggest you just keep single activity and use Fragments for different screen.

On Notification click you need to define entry point of app in notification payload and the entry point decide what will be the next navigation.

If you are having only single activity then you can define that activity as a entry point and on the activity you can decide do you have to push new fragment or not.

Or second option if you are using firebase then push all notification as background notification and onMessageReceive method you can get top activity from activity stack and set that activity as entry point for the notification. But there is still problem as user may be click on notification after navigate from set entry point activity which again problem. So I prefer to go with first one approach.




回答5:


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 app is running

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 that activity in notification intent.

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



回答6:


By this way also we can achieve the above 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();
        }


来源:https://stackoverflow.com/questions/41235142/how-to-open-current-activity-which-is-open-while-click-on-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!