简体   繁体   English

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

[英]Why isn't the public version of my notification displayed on the lock screen?

I'm using v7 of NotificationCompat to build a notification. 我正在使用NotificationCompat的v7来构建通知。 I want it to have a public version with less information for the lock screen, and a private version with more information for the notifications list available when the phone is unlocked. 我希望它有一个公共版本,锁定屏幕的信息较少,以及一个私人版本,当手机解锁时,可以获得通知列表的更多信息。 The instructions in the android developer documentation are pretty straight forward... But they're not working for me. android开发者文档中的说明非常简单......但它们并不适合我。 I get the private version all the time, even on the lock screen. 即使在锁定屏幕上,我也一直得到私人版本。

Can someone please tell me what I'm doing wrong? 有人可以告诉我我做错了什么吗?

I'm running android version 6.0.1 on my Samsung Galaxy S6, and for the private version of my notification I'm setting a custom view via the RemoteViews class. 我在我的三星Galaxy S6上运行Android版本6.0.1,对于我的通知的私有版本,我通过RemoteViews类设置自定义视图。

Here's my code: 这是我的代码:

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

I'm not sure exactly what fixed it, but it's working for me now using just standard notification actions. 我不确定究竟是什么修复它,但它现在只使用标准通知操作为我工作。 Here's what I have: 这就是我所拥有的:

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

And my base notification function: 我的基本通知功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM