如何使通知无法删除/不可删除

时间:2013-05-31 12:29:28

标签: android notifications

即使用户点击它或点击全部清除,我也想让我的Android通知停留...

现在它有时会停留,并且有时会被移除,我不确定是什么导致它。

这是我的通知代码:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void createNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                            .setContentTitle("Wolftech Field Agent")
                            .setContentText("Getting Status")
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setOngoing(true)
                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void updateNotificationText(String inString)
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                            .setContentText(inString)
                                            .setContentTitle("Wolftech Field Agent")
                                            .setSmallIcon(R.drawable.ic_launcher)
                                            .setOngoing(true)
                                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void cancelNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.cancel(NOTIFICATION_ID);
}

5 个答案:

答案 0 :(得分:9)

我认为您正在寻找持续通知,在这种情况下,如果您使用的是NotificationCompat.Builder,则可以使用:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setOngoing(true);

答案 1 :(得分:4)

我相信你正在寻找这面旗帜:

Notification.FLAG_NO_CLEAR

将该标记添加到您的通知中。

答案 2 :(得分:1)

你试试

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);
 notificationManager.cancel(pendingIntent);

答案 3 :(得分:1)

哦,我意识到我发布的代码实际上很好。

当我点击通知时,它再次调用onCreate(),然后在一个随机间隔后调用onDestroy()。

在onDestroy()中,我有了cancelNotification()方法,所以如果在onCreate()之后调用onDestroy(),它就会删除我的通知。

这给我带来了一个新问题:为什么在我按照我在这里找到的关于如何阻止这种情况发生的每一个答案后,它会破坏并重新创建我的活动?

如果你想帮助我解决问题,你可以在How to make notification resume and not recreate activity?找到这个问题......

答案 4 :(得分:-1)

我也有这个问题。我的解决方案是在创建意图的函数上添加额外的

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
puts "What type of train would u like to see today? steam or diesel"

train = gets.chomp

if train == "steam"
  driver.get 'https://en.wikipedia.org/wiki/Steam_locomotive'
elsif train == "diesel"
  driver.get 'https://en.wikipedia.org/wiki/Diesel_locomotive'
else "Sorry, I didn't understand that. Bye"
end

和MainActivity:

        private void addNotification(String headLine, String info, Context context) {
        mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)  //R.drawable.icon
                        .setContentTitle(headLine)
                        .setContentText(info)
                        .setOngoing(true)
                        .setAutoCancel(false);
// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, MainActivity.class);
        resultIntent.putExtra("FROM_NOTIF", true);
// 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.create(context);
// 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);

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        //      mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mNotificationManager.notify(mNotification_id, mBuilder.build());
    }

所以事情是用户按下它后通知会关闭,但是你可以通过onNewIntent()开始通知活动,检查那里的意图,如果你发现它来自通知,再设一次。