0

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 snippet,

@Override
public void onHandleIntent(Intent intent) {
 notificationManager(this, getString(R.string.new_ride), true);

}

public static void notificationManager(Context context, String message, boolean 
ring) {

        try {
            long when = System.currentTimeMillis();

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            Log.v("message",","+message);

            Intent notificationIntent = new Intent(context, SplashNewActivity.class);


            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setAutoCancel(true);
            builder.setContentTitle(context.getString(R.string.app_name));
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
            builder.setContentText(message);
            builder.setTicker(message);

            if(ring){
                builder.setLights(Color.GREEN, 500, 500);
            }
            else{
                builder.setDefaults(Notification.DEFAULT_ALL);
            }

            builder.setWhen(when);
            builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
            builder.setSmallIcon(R.drawable.notif_icon);
            builder.setContentIntent(intent);
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_ID, notification);
            //Open the App while new ride request arrives
            Intent dialogIntent = new 
            Intent(getBaseContext(),SplashNewActivity.class);                                            
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                           
            getApplication().startActivity(dialogIntent);
            } catch (Exception e) {
                            e.printStackTrace();
            }

}

Please let me know is it possible on all devices or it is device dependent. Thanks in advance.

Prashant
  • 66
  • 9
  • "I want to open app when device receive push notification from GCM same time app is already closed" -- the user may not appreciate your activity interrupting them in the middle of whatever they are doing. The user may express their dissatisfaction with you in many ways (poor ratings, physical violence, etc.). "I tried this code it works on some devices not on all devices" -- please explain **in detail** what "not on all devices" means. When you used your debugger, or logging, to see what happens... what happens? – CommonsWare May 09 '17 at 13:41

2 Answers2

1

I'm using this method inside Service class:

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

where

@SuppressLint("NewApi")
    public static Intent launchIntent(Context ctx) {
        final ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        Intent intent = new Intent();
        boolean activated = 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);
            String myPkgNm = ctx.getPackageName();
            if (!recentTaskInfos.isEmpty()) {
                ActivityManager.RecentTaskInfo recentTaskInfo;
                final int size = recentTaskInfos.size();
                for (int i = 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 = new Intent(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" />

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • not working....tried .i think device can't able to receive notification that's the main issue.how to get rid on this @Vyacheslav – Prashant May 10 '17 at 11:21
0

Try Alarm Manager. Hope this works!!

        Intent resultIntent = new Intent(this, SplashNewActivity.class);
        // This ensures that the back button follows the recommended
        // convention for the back key.
        TaskStackBuilder stackBuilder = 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);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);

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

call this code in your onMessageReceived method.

Chowdary102
  • 106
  • 2
  • 12
  • Elaborate your answer: Explain what this does, what line does what, and add more on how to use it – Zoe May 09 '17 at 13:49
  • tried not working..i think device cant able to receive notification that's the main issue. – Prashant May 10 '17 at 11:20
  • @Prashant FIrst make sure your device is getting GCM registration ID or not. And if it is not null and successfully saved in your server, check from server side whether the notification is getting delivered or it is throwing error. – Chowdary102 May 10 '17 at 12:18
  • 1
    Thanx, but when app is in background i'm getting notification means WakefulBroadcastReceiver class OnRecive methode get called. when closed then not get called . @Chowdary102 – Prashant May 10 '17 at 12:24