Skip to content Skip to sidebar Skip to footer

How To Open Android App On All Devices When Receive Push Notification (gcm) While App Is Closed?

I want to open app when device receive push notification from GCM same time app is already closed,I tried this code it works on some devices not on all devices. Below is my code sn

Solution 1:

I'm using this method inside Service class:

IntentmyAppIntent= launchIntent(this); 
           startActivity(myAppIntent);

where

@SuppressLint("NewApi")publicstatic Intent launchIntent(Context ctx) {
        finalActivityManageram= (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        Intentintent=newIntent();
        booleanactivated=false;


        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            List<ActivityManager.AppTask> tasks = am.getAppTasks();

            for (ActivityManager.AppTask task: tasks){
                intent = task.getTaskInfo().baseIntent;
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activated = true;
                break;
            }
        } else {
            @SuppressWarnings("deprecation")final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
            StringmyPkgNm= ctx.getPackageName();
            if (!recentTaskInfos.isEmpty()) {
                ActivityManager.RecentTaskInfo recentTaskInfo;
                finalintsize= recentTaskInfos.size();
                for (inti=0; i < size; i++) {
                    recentTaskInfo = recentTaskInfos.get(i);
                    if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
                        intent = recentTaskInfo.baseIntent;
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activated = true;
                    }
                }
            }
        }
        if (!activated) {
            intent = newIntent(ctx, YourDefaultActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        return intent;
    }

do not forget about permission (that is deprecated in new APIs but nonetheless is needed)

<uses-permission android:name="android.permission.GET_TASKS" />

Solution 2:

Try Alarm Manager. Hope this works!!

IntentresultIntent=newIntent(this, SplashNewActivity.class);
        // This ensures that the back button follows the recommended// convention for the back key.TaskStackBuilderstackBuilder= TaskStackBuilder.create(this);

        // Adds the back stack for the Intent (but not the Intent itself).
        stackBuilder.addParentStack(SplashNewActivity.class);

        // Adds the Intent that starts the Activity to the top of the stack.
        stackBuilder.addNextIntent(resultIntent);
        PendingIntentresultPendingIntent= stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);

        longfutureInMillis= System.currentTimeMillis();
        AlarmManageralarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, resultPendingIntent);

call this code in your onMessageReceived method.

Post a Comment for "How To Open Android App On All Devices When Receive Push Notification (gcm) While App Is Closed?"