2

I have to launch the app on receiving notification. The following piece of code works fine when the app is killed and notification is received (i.e the code inside if condition). But when the app is running in foreground or background, multiple instances of the activity gets created(i.e snippet in else condition). It's not the MainActivity that has to be launched on receiving the notification, instead it's some other activity containing the broadcast Receiver. I have added the following lines in the onMessage of GCMintentService class.

if (currentPackage.equalsIgnoreCase(context.getPackageName()
                .toString())) {
            broadcastMessage(context, message);
        } else {
            Intent mIntent = new Intent(context, MainActivity.class);
            mIntent.setAction(Intent.ACTION_MAIN);
            mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);
        }

In the activity, under onReceive method of BroadcastReceiver, i am starting the activity again.

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        startActivity(getIntent().setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
    }
};

1 Answers1

1

I also had this requirement in one of my application. We can achieve it if we call

Intent mIntent = new Intent(context, MainActivity.class);
                mIntent.setAction(Intent.ACTION_MAIN);
                mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);
broadcastMessage(context, message);

In the main activity use the following in the broadcast receiver that would receive the broadcasted message above.

WakeLock wakeLock = null;
        KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = kgMgr.inKeyguardRestrictedInputMode();
        PowerManager pm = (PowerManager) context
                .getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakeLock");
            wakeLock.acquire();
        }
        if (locked) {
            Window mWindow = getWindow();
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        } 

Personally I feel that this is not the best of the answers and also best of the ideas to open the app directly when received a notification as there will be many functions like onCreate onResume, will be triggered automatically, spoil the users work if they are in a really important work by opening another app directly, also we need to put a lot of flags or use any other method to manage the flow of the application, when user open the app, app comes from background, app opened due notification and all such cases. Avoid it as it spoils the whole user experience.