未显示应用程序图标徽章 Android
Application icon badge not displayed Android
对于一项作业,我必须编写一个可以显示所有联系人及其生日日期(以及创建/修改它们)的应用程序。到目前为止,一切都很好。然后,我必须向今天出生的所有人发出通知。
通知启动没有问题,但我没有看到应用程序图标徽章。
搜索后,我发现我必须使用频道 -> 完成。
我已经在设置中验证了通知点是否启用:
我已经验证 android 文档说 "it's automatic you shoundn't have anything to do"
我的模拟器使用的是 Android 9.0,它比 android oreo 更高。
怎么了?
public class BirthdayListener implements View.OnClickListener {
private static final String CHANNEL_ID = "ch.hefr.tic.birthday_channel";
private static final int SUMMARY_ID = -1;
private static final String GROUP_KEY = "ch.hefr.tic.group_channel";
private Context context;
private NotificationManager notificationManager;
public BirthdayListener(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getResources().getString(R.string.channel_name);
String description = context.getResources().getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@Override
public void onClick(View v) {
List<Contact> hasBirthday = searchBirthdays();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
NotificationCompat.InboxStyle summaryStyle = new NotificationCompat.InboxStyle();
summaryStyle.setBigContentTitle(hasBirthday.size() + " new messages");
for(int i = 0; i < hasBirthday.size(); ++i) {
Contact contact = hasBirthday.get(i);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Birthday")
.setContentText(contact.getName() + " has his birthday today !")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroup(GROUP_KEY)
.setNumber(5);
String phoneNumber = contact.getPhoneNumber();
if(phoneNumber != null && !phoneNumber.equals("")) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:" + phoneNumber));
PendingIntent pending = PendingIntent.getActivity(context, 0, smsIntent, 0);
builder.addAction(R.drawable.ic_arrow_back, "SMS", pending);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationManagerCompat.notify(i, builder.build());
else
notificationManager.notify(i, builder.build());
}
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Today Birthdays")
.setContentText(hasBirthday.size() + " new messages")
.setStyle(summaryStyle)
.setGroup(GROUP_KEY)
.setGroupSummary(true)
.setNumber(5);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationManagerCompat.notify(SUMMARY_ID, summaryBuilder.build());
else
notificationManager.notify(SUMMARY_ID, summaryBuilder.build());
}
}
编辑:尝试在 NotificationCompat.Builder 生成器上使用 setColor,但它改变了 the color of the icon。仍然没有任何徽章。
感谢 Bob 和他的 link : https://support.google.com/pixelphone/thread/1575301?hl=en,
我发现我必须转到设置 < 应用和通知 < 特殊应用访问 < 通知 < 通知访问 < 打开 "pixel launcher"。然后通知点将按预期出现!
我想是时候写我的报告了。
对于一项作业,我必须编写一个可以显示所有联系人及其生日日期(以及创建/修改它们)的应用程序。到目前为止,一切都很好。然后,我必须向今天出生的所有人发出通知。
通知启动没有问题,但我没有看到应用程序图标徽章。
搜索后,我发现我必须使用频道 -> 完成。
我已经在设置中验证了通知点是否启用:
我已经验证 android 文档说 "it's automatic you shoundn't have anything to do"
我的模拟器使用的是 Android 9.0,它比 android oreo 更高。
怎么了?
public class BirthdayListener implements View.OnClickListener {
private static final String CHANNEL_ID = "ch.hefr.tic.birthday_channel";
private static final int SUMMARY_ID = -1;
private static final String GROUP_KEY = "ch.hefr.tic.group_channel";
private Context context;
private NotificationManager notificationManager;
public BirthdayListener(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getResources().getString(R.string.channel_name);
String description = context.getResources().getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@Override
public void onClick(View v) {
List<Contact> hasBirthday = searchBirthdays();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
NotificationCompat.InboxStyle summaryStyle = new NotificationCompat.InboxStyle();
summaryStyle.setBigContentTitle(hasBirthday.size() + " new messages");
for(int i = 0; i < hasBirthday.size(); ++i) {
Contact contact = hasBirthday.get(i);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Birthday")
.setContentText(contact.getName() + " has his birthday today !")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroup(GROUP_KEY)
.setNumber(5);
String phoneNumber = contact.getPhoneNumber();
if(phoneNumber != null && !phoneNumber.equals("")) {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:" + phoneNumber));
PendingIntent pending = PendingIntent.getActivity(context, 0, smsIntent, 0);
builder.addAction(R.drawable.ic_arrow_back, "SMS", pending);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationManagerCompat.notify(i, builder.build());
else
notificationManager.notify(i, builder.build());
}
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Today Birthdays")
.setContentText(hasBirthday.size() + " new messages")
.setStyle(summaryStyle)
.setGroup(GROUP_KEY)
.setGroupSummary(true)
.setNumber(5);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
notificationManagerCompat.notify(SUMMARY_ID, summaryBuilder.build());
else
notificationManager.notify(SUMMARY_ID, summaryBuilder.build());
}
}
编辑:尝试在 NotificationCompat.Builder 生成器上使用 setColor,但它改变了 the color of the icon。仍然没有任何徽章。
感谢 Bob 和他的 link : https://support.google.com/pixelphone/thread/1575301?hl=en, 我发现我必须转到设置 < 应用和通知 < 特殊应用访问 < 通知 < 通知访问 < 打开 "pixel launcher"。然后通知点将按预期出现!
我想是时候写我的报告了。