为什么锁定屏幕上显示我的通知的公开版本?

时间:2017-01-01 23:51:06

标签: java android android-notifications

我使用NotificationCompat的v7来构建通知。我希望它有一个公共版本,锁定屏幕的信息较少,以及一个私人版本,当手机解锁时,可以获得通知列表的更多信息。 android开发者文档中的说明非常简单......但他们并不适合我。即使在锁定屏幕上,我也一直得到私人版本。

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

我在Samsung 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());

1 个答案:

答案 0 :(得分: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));
}