【问题标题】:create an "ongoing" notification android [duplicate]创建一个“正在进行的”通知android [重复]
【发布时间】:2017-02-09 02:51:03
【问题描述】:

我找不到创建“持续”通知的方法 - 实际上我不确定这种通知是否被称为这种方式 - 如下图所示:

Grey Notification Messenger

我如何通过我的通知 (Pinger) 实现这一目标?我的通知是为前台服务创建的,所以如果我的要求是可能的,我不会拒绝。

感谢您的帮助。

编辑:您可以在这里找到的答案 (Android: How to create an "Ongoing" notification?) 并没有解决我的问题。这个问题不是重复的。

我的代码:

Intent showTaskIntent = new Intent(getApplicationContext(), BackingService.class);
    showTaskIntent.setAction(Intent.ACTION_MAIN);
    showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Intent detailsIntent = new Intent(BackingService.this, DetailsActivity.class);
    detailsIntent.putExtra("EXTRA_DETAILS_ID", 42);
    PendingIntent contentIntent = PendingIntent.getActivity(
            getApplicationContext(),
            0,
            showTaskIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent replyPendingIntent = null;
    if (Build.VERSION.SDK_INT < 24) {
        replyPendingIntent = contentIntent;
    } else {
        replyPendingIntent = PendingIntent.getBroadcast(
                BackingService.this,
                0,
                new Intent(BackingService.this, FromNotifSender.class),
                PendingIntent.FLAG_UPDATE_CURRENT
        );
    }
    RemoteInput remoteInput = new RemoteInput.Builder(KEY_NOTIFICATION_REPLY)
            .setLabel("Tapez votre message ici")
            .build();
    android.support.v4.app.NotificationCompat.Action replyAction = new android.support.v4.app.NotificationCompat.Action.Builder(
            0, "Envoyer à "+usernamefriend, replyPendingIntent)
            .addRemoteInput(remoteInput)
            .build();
    android.support.v4.app.NotificationCompat.Builder miBuilder = new NotificationCompat.Builder(BackingService.this)
            .setSmallIcon(R.drawable.logo4)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(contentText)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent)
            .setAutoCancel(false)
            .setOngoing(true)
            .setColor(getResources().getColor(R.color.colorPrimaryDark));
    if (Build.VERSION.SDK_INT < 24) {
    } else {
        if(usernamefriend.equalsIgnoreCase("un ami")){

        }else{
            miBuilder.addAction(replyAction);
        }

    }
    Intent resultIntent = new Intent(BackingService.this, SearchActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(BackingService.this);
    stackBuilder.addParentStack(SearchActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    miBuilder.setContentIntent(resultPendingIntent);
    startForeground(002, miBuilder.build());

如您所见,.setOngoing(true) 对我不起作用。

解决方案

您只需将通知的优先级设置为 Notification.PRIORITY_MIN,如下面的代码所示:

android.support.v4.app.NotificationCompat.Builder miBuilder = new NotificationCompat.Builder(BackingService.this)
            .setSmallIcon(R.drawable.logo4)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(contentText)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent)
            .setAutoCancel(false)
            .setOngoing(true)
            .setColor(getResources().getColor(R.color.colorPrimaryDark))
            .setPriority(Notification.PRIORITY_MIN);

【问题讨论】:

  • 请确保在您的问题中发布一些代码,不要过于宽泛,我建议您访问 Stackoverflow 帮助中心以了解有关提问的更多信息
  • 喜欢这个问题是如何被标记为重复的,即使这个“重复”问题已经 6 岁了,而且移动开发是一个非常不稳定的领域。

标签: java android notifications


【解决方案1】:
public void showNotification() {
 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 NotificationManager nMN = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 Notification n  = new Notification.Builder(this)
 .setContentTitle("Notification Title")
 .setContentText("Notification content")
 .setSmallIcon() //set your icon
 .setVibrate(new long[] { 1000, 1000 })
 .setLights(Color.BLUE, 700, 500)
 .setSound(alarmSound)
 .setOngoing(true)
 //.setStyle(new NotificationCompat.BigTextStyle(NorProv))
 .build();
 nMN.notify(NOTIFICATION_ID, n);
}

更新

Notification.FLAG_ONGOING_EVENT 标志分配给您的通知,如下所示:

notif.flags = Notification.FLAG_ONGOING_EVENT;

更新 #2

这用于设置通知的优先级:

.setPriority(Notification.PRIORITY_LOW);
 //other priorities: MIN, HIGH, DEFAULT, MAX

要隐藏图标,只需尝试删除 .setSmallIcon() 并仅使用 .setLargeIcon()

【讨论】:

  • .setOngoing(true) 没有解决我的问题,我仍然收到相同的通知
  • 如果你刷它,通知会消失吗?
  • 检查更新的答案
  • 不,它没有,但是当我刷它时我的通知没有被取消。我正在寻找一种将我的通知作为 Messenger 通知(在图片中)的方法。
  • 更新后的答案也不起作用。我仍然收到相同的通知。也许这是不可能的?
【解决方案2】:

这就是我所做的。当您运行前台服务时,它会强制您获得持久/持续的通知。如果你不这样做,那么你没有运行前台服务。诀窍是 onStartCommand() 覆盖和 startsticky 命令。这应该在您的服务范围内。使用 onStartCommand 而不是 onCreate。并确保启动Foreground。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    /*
    Do Work
    */

    Notification notification = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_image)
        .setContentText("Display Text")
        .setPriority(Notification.PRIORITY_LOW).build();

    startForeground(MyConstants.NOTIF_SERVICE, notification);
    return START_STICKY;
}

【讨论】:

  • 对不起,我是初学者...你能告诉我什么是 MyConstants 和 receiver 吗?
  • 我的服务是拿着一个接收器来对抗我最终不需要的东西。我为你修改了帖子。 MyConstants 是我的单例,它保存我的常量,因为通知需要通知 ID。您可以替换 MyConstants.NOTIF_SERVICE' with any number like 001` 或其他任何内容。只要您有启动前台和它的链接通知,您在onStartCommand 中启动的任何内容都应该保持活动状态。我说应该是因为我不知道你的服务在做什么。
  • 我想一个更好的问题是你到底想要这个通知做什么。
  • 我希望此通知低于所有其他通知,即使它是在之后创建的,并且我希望能够在通知状态栏上隐藏它的图标
  • 但这也必须是您在前台运行的服务?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多