gpt4 book ai didi

java - 为什么我的通知的公共(public)版本没有显示在锁定屏幕上?

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:41 24 4
gpt4 key购买 nike

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

有人可以告诉我我做错了什么吗?

我在 Samsung Galaxy S6 上运行 Android 版本 6.0.1,对于我的通知的私有(private)版本,我通过 RemoteViews 类设置自定义 View 。

这是我的代码:

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

最佳答案

我不确定到底是什么解决了这个问题,但它现在只使用标准通知操作就对我有用。这是我所拥有的:

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

关于java - 为什么我的通知的公共(public)版本没有显示在锁定屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com