为什么我的通知在锁屏界面上没有显示公共版本?

3
我正在使用v7的NotificationCompat构建通知。我希望它有一个公共版本,锁屏时显示较少的信息,并且有一个私人版本,在手机解锁时可以查看更多信息的通知列表中可用。 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());

公共通知仅适用于安全锁屏,即需要图案或密码的锁屏。你的手机锁屏了吗? - Oleg Bogdanov
我的手机需要用指纹解锁,但我认为这应该算作锁屏。 - SnoopDougg
1
值得一提的是,我的这个样例应用程序似乎运行良好,尽管我没有设置一个带有指纹锁的设备。我看到最大的区别是RemoteViews。我不认为它会弄乱事情,但我不能排除这种可能。 - CommonsWare
唉,谢谢,但我尝试删除setContent并改用标准的标题和文本,我尝试回滚到NotificationCompat的v4版本,就像您的示例一样,确保为公共和私有设置了内容意图,也从您的示例中复制了setDefaults方法,并尝试显式声明公共通知的可见性为NotificationCompat.VISIBILITY_PUBLIC,但似乎没有任何作用。我总是在锁屏上收到私人通知:( - SnoopDougg
也许安卓版本6.0.1不支持这个?虽然我记得在通知文档中看到过,这应该能够向后兼容到安卓5。 - SnoopDougg
1
我在这方面遇到了一些麻烦。折腾了一个小时后,我意识到我必须将设备中的通知设置为“隐藏敏感内容”。这可能与您的问题无关,但可能会帮助那些可能会遇到类似问题的人。它也适用于使用RemoteViews的通知。 - Supreethks
1个回答

0

我不确定是什么解决了它,但现在使用标准通知操作对我有效。以下是我的代码:

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

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