Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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(); 
like image 897
sunil Avatar asked Dec 20 '16 04:12

sunil


3 Answers

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);
like image 123
Kevin Krumwiede Avatar answered Oct 21 '22 15:10

Kevin Krumwiede


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();
}
like image 41
sunil Avatar answered Oct 21 '22 16:10

sunil


You should have something like this in Application class to store the current activity.

private BaseActivity mCurrentActivity = null;

public BaseActivity getCurrentActivity() {
    return mCurrentActivity;
}

public void setCurrentActivity(BaseActivity currentActivity) {
    this.mCurrentActivity = currentActivity;
}

Then, inside your handle notification Service class.

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

    BaseActivity currentActivity = ((App) this.getApplicationContext())
                               .getCurrentActivity();
        Intent intent;
        if (currentActivity instanceof ActivityA) {
            intent = new Intent(this, ActivityA.class);
        } else if (currentActivity instanceof ActivityB) {
            intent = new Intent(this, ActivityB.class);
        } else {
            intent = new Intent(this, MainActivity.class);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

    // your code...
}

If your app is killed, default activity will be called, such as MainActivity.

Else, when you receive & click push notification message when app is on foreground or background. Current activity will stay there as default activity, such as ActivityA & ActivityB. Then you can navigate to wherever another activities or fragments.

My suggestion, better we should use Fragment, it's easier in navigate to specially screen from push notification.

like image 38
Tri Ngo Minh Avatar answered Oct 21 '22 15:10

Tri Ngo Minh