【问题标题】:make notification clickable and undestroyable使通知可点击且不可破坏
【发布时间】:2013-08-30 14:52:02
【问题描述】:

我试图创建一个通知,通过单击它来启动一个活动,并且你不能滑动它。

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("DroidSchool")
                .setContentText("DroidSchool l\u00E4uft im Hintergrund...");

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
        int mId = 1234567890;
        mNotificationManager.notify(mId, mBuilder.build());

上面的代码显示了通知,但是当我点击它时没有任何反应,你可以将它滑开。

【问题讨论】:

  • 问题主要在于意图......
  • 如果它是不可破坏的,那你怎么能把它移除呢?
  • 应用程序关闭时@bofredo。我的意思是例如像 avast 通知
  • 没试过,但也许a foreground service是你要找的?
  • 如果您使用带有通知的服务,您应该使用 ForegroundService !!

标签: java android android-intent notifications android-pendingintent


【解决方案1】:

要保留通知,这样用户就不能以任何方式点击它,添加这个标志

Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);

要启动 Activity,您必须将此标志用于 Intent

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

【讨论】:

  • @NathanMaier 此代码是否在服务中运行?您想通过单击哪个 Activity 开始,离开应用程序时的上一个?
  • 是的,它在服务中,但现在可以使用(我添加了noti.contentIntent = pendingIntent;
【解决方案2】:

这样做:

 NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("DroidSchool")
            .setContentText("DroidSchool l\u00E4uft im Hintergrund...");

    Intent intent = new Intent(YourActivity.this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


   Notification noti = mBuilder.build();

   noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;

   NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.notify(0, noti);  

【讨论】:

    【解决方案3】:

    您需要详细的前台服务here

    值得注意的是,只有 android 4.3 添加了您似乎需要的持久通知,并且可以被用户设置覆盖。

    【讨论】:

      【解决方案4】:

      尝试这样使用

      Intent intent = new Intent(this, MainActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
      

      在 MainActivity.java 中

      不要消费你的通知;即,而不是在 onDestro() 下的 oncreate() 调用中调用以下 2 行代码

       NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          nm.cancel(mId);
      

      【讨论】:

        【解决方案5】:

        你可以使用NotificationCompat.Builder.setOngoing(true),这样使用:

        NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
                                                             .setAutoCancel(true)
                                                             .setContentTitle("Downloading Something")
                                                             .setContentText("Downloading")
                                                             .setSound(null)
                                                             .setDefaults(0)
                                                             .setOngoing(true);
        // Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-08
          • 2019-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多