如何使通知无法取消或移除?

7

我希望我的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个回答

11

我认为你正在寻找持续通知,如果是这种情况,如果你使用NotificationCompat.Builder,那么你可以使用:

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

这必须是前台服务吗? - IgorGanapolsky

4
我相信您正在寻找这个标志:
Notification.FLAG_NO_CLEAR

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

当我点击清除所有按钮时,它似乎保留着,但有时候当我点击它时它仍然消失了。很奇怪。 - CeeRo
现在我倾向于在onResume()上重新创建/更新通知,以便在每次单击后重新制作它,否则似乎没有办法确保它真的留下来... - CeeRo
我找出了导致这个问题的原因,请查看我的答案。 - CeeRo
2
这两者有什么区别:thissetOngoing(true) - IgorGanapolsky

1

你尝试

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

我认为你误解了问题(或者是我误解了你的回答...)。我不想取消通知(好吧,当我决定时我会这么做),我想让通知在用户每次点击它或点击“全部清除”时保持显示。 - CeeRo

1

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

发生的情况是,当我点击通知时,它会再次调用onCreate(),然后在随机时间间隔后调用onDestroy()。

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

这带来了一个新问题:为什么它会在我已经按照这里找到的所有答案停止它发生的方式后销毁并重新创建我的活动?

如果您想帮助我解决这个问题,您可以在这里找到该问题 如何使通知恢复而不重新创建活动? ...


-1

我也遇到了这个问题。我的解决方案是在创建意图的函数中添加额外的内容。

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

在 MainActivity 中:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getBooleanExtra("FROM_NOTIF", false)) {
        addNotification(...)
            ... call the function that adds the notification again ..}

问题在于用户点击通知后,通知会关闭,但是您可以使用onNewIntent()在启动的活动中接收通知,检查意图,如果发现它来自通知,则重新设置它。


虽然这段代码可能回答了问题,但提供有关它如何以及/或为什么解决问题的附加上下文将改善答案的长期价值。请阅读此如何回答以提供高质量的答案。 - thewaywewere
修改了我的回答。 - Itai.S.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接