如何解决棒棒糖上未显示推送通知内容文本
// receiverUid = unique uid for a receiver
// notificationUid = unique notification_uid
// receiverName,title,body are String variables.
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(this,"NOTIFICATION_CHANNEL")
.setSmallIcon(R.drawable.ic_app_icon_24)
.setColor(ColorUtils.getColor(getApplicationContext(),R.color.blue_700))
.setGroupSummary(true)
.setGroup(receiverUid)
.setAutoCancel(true)
.setSubText(receiverName)
.setContentIntent(pendingIntentGroup);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this,R.color.blue_700))
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntentNotification)
.setGroup(receiverUid)
.setSubText(receiverName)
.setAutoCancel(true);
notificationmanagerCompat notificationmanager = notificationmanagerCompat.from(this);
// group notification is based on receiverUid,so the notifications are grouped under different receipients
notificationmanager.notify(receiverUid,groupBuilder.build());
notificationmanager.notify(receiverUid + "_" + notificationUid,builder.build());
这在更高的 android 版本上工作正常,但对于 Lollipop 版本不显示通知文本。这是 Lollipop 设备上的屏幕截图:
我调试并验证了文本传递到这里。请建议。提前致谢。
解决方法
来自官方doc:
设置小组总结
在 Android 7.0(API 级别 24)及更高版本上,系统会自动 使用来自每个小组的文本片段为您的小组构建摘要 通知。用户可以展开此通知以查看每个 单独通知,如图1所示。 支持旧的 版本,不能显示一组嵌套的通知,您必须 创建一个额外的通知作为摘要。这显示为 唯一的通知,系统隐藏所有其他通知。所以这 摘要应包括所有其他通知的片段, 用户可以点击它来打开您的应用
文本未显示,因为 groupBuilder 是显示的那个并且它不包含任何文本,因此对于较低版本的 API 27 将您的文本添加到 groupBuilder 或创建一个样式来总结其他通知的内容例如:
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(this,"NOTIFICATION_CHANNEL")
.setSmallIcon(R.drawable.ic_app_icon_24)
.setColor(ColorUtils.getColor(getApplicationContext(),R.color.blue_700))
.setGroupSummary(true)
.setGroup(receiverUid)
.setAutoCancel(true)
.setSubText(receiverName)
.setContentIntent(pendingIntentGroup)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faarborg Check this out")
.setBigContentTitle("2 new messages")
.setSummaryText("janedoe@example.com"));
更新: 在样式中实现通知计数和消息摘要
public class MainActivity extends AppCompatActivity {
private int notificationCount = 0;
private NotificationCompat.InboxStyle style;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// also you can Save the Notification count on the sharePreference
// for simulate the notification with multiple message
String[][] person = {{"Bill","Jones"},{"Janet","Kline"},{"George","Bailey"},{"Ellan","Sanches"},{"Tom","Nguyen"},{"William","Walters"},{"Author","James"},{"Henry","Daniels"},{"Mike","Franklin"},{"Julie","Andrews"}};
// i have just testing with the TimeDownCount to add notifications every 20 seconds
CountDownTimer countDownTimer = new CountDownTimer(2000,10) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
// get random person from the array person
Random random = new Random();
int randomInt = random.nextInt(person.length - 1);
// show notification
showNotification(person[randomInt][0] + " " + person[randomInt][1]);
if (notificationCount < 10) {
this.start();
}
}
};
countDownTimer.start();
style = new NotificationCompat.InboxStyle();
}
private void showNotification(String message) {
notificationCount++;
style.addLine(message); // add person to the style
style.setBigContentTitle(notificationCount + " new messages"); // show Notification count in the Title
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(this,"NOTIFICATION_CHANNEL")
.setSmallIcon(R.drawable.ic_baseline_mic_24)
.setColor(Color.BLUE)
.setGroupSummary(true)
.setGroup("receiverUid")
.setAutoCancel(true)
.setStyle(style)
.setSubText("Your Action");
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this,"NOTIFICATION_CHANNEL")
.setSmallIcon(R.drawable.ic_baseline_mic_24)
.setColor(Color.BLUE)
.setContentTitle("title")
.setContentText("body")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroup("receiverUid")
.setSubText("receiverName")
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify("receiverUid",groupBuilder.build());
notificationManager.notify("receiverUid" + "_" + "notificationUid",builder.build());
}
}
代码工作原理概述:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。