Android通知恢复活动

问题描述 投票:0回答:5

我正在尝试在用户暂停我的应用程序时发出通知。因此,为了更容易,用户可以使用通知快速转到应用程序。这是我正在使用的代码。它适用于 android 4 之前的所有版本,我不知道是哪个问题

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)    
                .setContentTitle("Titulo")
                .setContentText("Titulo");

        mBuilder.setOngoing(true);

        // Creates an explicit intent for an Activity this 
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        // put the flags
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.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
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

所以当我在 android 4.0 及更高版本中按下通知时,将再次创建活动而不是恢复。请任何帮助,我无法让它工作。

编辑(忘记清单单顶)

android:launchMode="singleTop"
同样的结果,不工作...

我的活动包含一张地图。我正在使用新版本的谷歌地图。 v2.

android notifications flags
5个回答
3
投票

我刚刚尝试了 PendingIntent.FLAG_CANCEL_CURRENT 并且似乎对我有用

public void showNotification(String header,String message){

            // define sound URI, the sound to be played when there's a notification
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Intent intent = new Intent(this, MainActivity.class);

            //PendingIntent.FLAG_CANCEL_CURRENT will bring the app back up again
            PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,PendingIntent.FLAG_CANCEL_CURRENT, intent, 0);

            Notification mNotification = new Notification.Builder(this)
                .setContentTitle(header)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setSound(soundUri)

                .addAction(R.drawable.ic_launcher, "View", pIntent)
                .addAction(0, "Remind", pIntent)
                .setOngoing(true)//optional
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(0, mNotification);
}

2
投票

android:launchMode="singleTop"
 的清单声明中使用 
MainActivity


2
投票

经过大量搜索后,唯一真正对我有用的解决方案是执行以下操作:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).set...(...).set...(..);

Intent resultIntent = new Intent(this, MainClass.class);
resultIntent.setAction("android.intent.action.MAIN");
resultIntent.addCategory("android.intent.category.LAUNCHER");

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());

这将打开您当前的活动而无需创建另一个活动!


0
投票

@Patrick 提供的解决方案取自问题并添加为答案:

NotificationCompat.Builder mBuilder = 
                new NotificationCompat.Builder(this) 
                .setSmallIcon(R.drawable.logo)    
                .setContentTitle(getString(R.string.txt)) 
                .setContentText(getString(R.string.txt)); 

        mBuilder.setOngoing(true); 

        // Creates an explicit intent for an Activity in your app 
        Intent resultIntent = new Intent(this, Activity.class); 

        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

        // The stack builder object will contain an artificial back stack for the 
        // started Activity. 
        // This ensures that navigating backward from the Activity leads out of 
        // your application to the Home screen. 
        TaskStackBuilder stackBuilder = TaskStackBuilder.from(this); 
        // Adds the back stack for the Intent (but not the Intent itself) 
        stackBuilder.addParentStack(Activity.class); 
        // Adds the Intent that starts the Activity to the top of the stack 
        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); 
        mBuilder.setContentIntent(resultPendingIntent); 
        NotificationManager mNotificationManager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        // mId allows you to update the notification later on. 
        mNotificationManager.notify(mId, mBuilder.getNotification());

0
投票

解决方案似乎不适用于版本 33,

FLAG_CANCEL_CURRENT
不可用。

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

© www.soinside.com 2019 - 2024. All rights reserved.