在关闭应用程序时收到推送通知(GCM)时,如何在所有设备上打开android应用程序? [英] How to open android app on all devices when receive push notification (GCM) while app is closed?

查看:133
本文介绍了在关闭应用程序时收到推送通知(GCM)时,如何在所有设备上打开android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设备从GCM接收到推送通知的同时打开应用程序,同时应用程序已经关闭,我尝试了此代码,该代码适用于某些设备,而不是在所有设备上. 以下是我的代码段

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.

推荐答案

我正在Service class中使用此方法:

I'm using this method inside Service class:

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

其中

@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;
    }

不要忘记许可(新API中已弃用了许可,但仍然需要)

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

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

这篇关于在关闭应用程序时收到推送通知(GCM)时,如何在所有设备上打开android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆