【问题标题】:How to make notification uncancellable/unremovable如何使通知不可取消/不可删除
【发布时间】:2017-06-18 21:40:31
【问题描述】:

我想让我的 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);
}

【问题讨论】:

    标签: android notifications


    【解决方案1】:

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

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

    【讨论】:

    • 这必须是前台服务吗?
    【解决方案2】:

    我相信你正在寻找这个标志:

    Notification.FLAG_NO_CLEAR
    

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

    【讨论】:

    • 我点击全部清除时它似乎仍然存在,但当我点击它时它有时仍然消失。奇怪
    • 现在我倾向于重新创建/更新 onResume() 上的通知,以便在每次点击后重新制作,似乎没有办法确保它真的保持不变。 ..
    • 我知道是什么原因,检查我的答案
    • 这和setOngoing(true)有什么区别?
    【解决方案3】:

    你试试

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

    【讨论】:

    • 我认为你误解了这个问题(或者我误解了你的回答......)我不想取消通知(好吧,我想,当我决定时),我想做通知停留在用户点击它或点击“全部清除”的每个计时器。
    【解决方案4】:

    好吧,我发现我发布的代码确实不错。

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

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

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

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

    【讨论】:

      【解决方案5】:

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

              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() 的活动上收到通知,检查那里的意图,如果你发现它来自通知,重新设置。

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读此how-to-answer 以提供高质量的答案。
      • 编辑了我的答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2018-10-21
      • 2016-04-17
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多