Android open activity upon notification

I need to run the application when I receive a notification. The following code fragment works fine when the application is killed and a notification is received (i.e., the code inside the if). But when the application runs in the foreground or in the background, several instances of activity are created (for example, a fragment in a different state). This is not a MainActivity that should be triggered when a notification is received; instead, it is another action that contains a broadcast receiver. I added the following lines to the onMessage class of the 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 my activity under the onReceive BroadcastReceiver method, I run the action again.

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

I also had this requirement in one of my applications. We can achieve this 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 action, use the following in the broadcast receiver, which will receive the transmitted 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 answer, as well as the best ideas to open the application directly when you receive a notification, since there will be many functions, such as onCreate onResume, that will automatically start, ruining the work of users if they are in a really important job When opening another application directly, we also need to set many flags or use any other method to control the flow of the application, when the user opens the application, the application comes from the background, the application opens from and notice all such cases. Avoid it as it spoils the entire user experience.

0
source

All Articles