【问题标题】:Why isn't the public version of my notification displayed on the lock screen?为什么我的通知的公开版本没有显示在锁定屏幕上?
【发布时间】:2017-01-01 23:51:06
【问题描述】:

我正在使用 NotificationCompat v7 来构建通知。我希望它有一个公开版本,锁屏信息较少,而私人版本则在手机解锁时提供更多通知列表信息。 android 开发人员文档中的说明非常简单......但它们对我不起作用。我一直得到私人版本,即使在锁定屏幕上也是如此。

谁能告诉我我做错了什么?

我在三星 Galaxy S6 上运行 android 版本 6.0.1,对于通知的私人版本,我正在通过 RemoteViews 类设置自定义视图。

这是我的代码:

NotificationCompat.Builder publicNotificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setContentTitle(expense.name)
                .setContentText(expense.amount)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.wally_icon)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.wally_icon)
                .setContent(remoteViews)
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                .setPublicVersion(publicNotificationBuilder.build())
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
notificationManager.notify((int)expense.id, notificationBuilder.build());

【问题讨论】:

  • 公开通知仅适用于安全锁屏,即需要图案或别针的锁屏,您的锁屏了吗?
  • 我的必须用我的指纹解锁,但我原以为这会算作锁屏。
  • FWIW, this sample app of mine 似乎工作正常,尽管我没有在锁屏上设置指纹锁的设备。我看到的最大区别是RemoteViews。我不认为这会把事情搞砸,但我不能排除它。
  • 呃,谢谢,但我尝试删除 setContent 并改用标准标题和文本,我尝试像您的示例一样恢复到 NotificationCompat 的 v4,确保为两者设置了内容意图公共和私有,也从您的示例中复制了 setDefaults 方法,并尝试将公共通知的可见性明确声明为 NotificationCompat.VISIBILITY_PUBLIC,但似乎没有任何效果。我总是在锁定屏幕上收到私人通知:(
  • 我遇到了一些麻烦。摆弄了一个小时后,我意识到我必须在设备中设置通知设置以“隐藏敏感内容”。它可能与您的问题无关,但可能会帮助可能登陆这里并遇到与我类似问题的人。它也适用于RemoteViews 的通知。

标签: java android android-notifications


【解决方案1】:

我不确定究竟是什么修复了它,但它现在只使用标准通知操作对我有用。这是我所拥有的:

Intent saveAmountIntent = new Intent(context, SaveAmountReceiver.class);
Intent changeAmountIntent = new Intent(context, ChangeAmountReceiver.class);
saveAmountIntent.putExtra("expenseId", expense.id);
changeAmountIntent.putExtra("expenseId", expense.id);
PendingIntent saveAmountPendingIntent = PendingIntent.getBroadcast(context, 0, saveAmountIntent, 0);
PendingIntent changeAmountPendingIntent = PendingIntent.getBroadcast(context, 0, changeAmountIntent, 0);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder publicNotificationBuilder = (NotificationCompat.Builder) this.createBaseNotification(context, expense, saveAmountPendingIntent)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) this.createBaseNotification(context, expense, saveAmountPendingIntent)
    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
    .setPublicVersion(publicNotificationBuilder.build())
    .addAction(new NotificationCompat.Action.Builder(R.drawable.change_icon, context.getString(R.string.change), changeAmountPendingIntent).build())
    .addAction(new NotificationCompat.Action.Builder(R.drawable.save_icon, context.getString(R.string.save), saveAmountPendingIntent).build());
notificationManager.notify((int)expense.id, notificationBuilder.build());

还有我的基本通知功能:

private NotificationCompat.Builder createBaseNotification(Context context, Expense expense, PendingIntent deleteIntent) {
    return (NotificationCompat.Builder) new NotificationCompat.Builder(context)
        .setContentTitle(expense.name)
        .setContentText(expense.amount)
        .setDeleteIntent(deleteIntent)
        .setAutoCancel(false)
        .setSmallIcon(R.drawable.my_notif_icon)
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多