如何在应用程序关闭时接收推送通知(GCM)时在所有设备上打开Android应用程序?

时间:2017-05-09 13:35:57

标签: android android-activity

我想在设备从GCM收到推送通知时打开应用程序同时应用程序已关闭,我尝试了此代码,它可以在某些设备上运行,而不是在所有设备上。 以下是我的代码段

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

}

请让我知道是否可以在所有设备上使用,或者它取决于设备。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我在Service class中使用此方法:

val a = Ais(Ais.Flag.NotFlagged, Ais.Unknown, false)
val b = a.copy(visited = true)

,其中

$api->post('update/{id}', 'App\Http\Controllers\UserController@update');
$api->post('update/{id}', 'App\Http\Controllers\HomeController@update');

不要忘记权限(在新API中已弃用但仍然需要)

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

答案 1 :(得分:0)

尝试使用Alarm Manager。希望这有效!!

        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);

在onMessageReceived方法中调用此代码。

相关问题