对于一项作业,我必须编写一个应用程序,该程序可以显示所有联系人及其生日(并创建/修改)。到目前为止,一切都很好。然后,我必须向所有今天出生的人发出通知。
启动通知没有问题,但是我没有获得应用程序图标徽章。
搜索后,我发现必须使用频道->完成。
我已在设置中验证是否启用了通知点:
我已经验证了android文档,说“您无须理会是自动的”
我的模拟器正在使用比android oreo更大的Android 9.0。
那怎么了?
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。仍然没有任何徽章。
答案 0 :(得分:0)
感谢Bob和他的链接:https://support.google.com/pixelphone/thread/1575301?hl=en, 我发现我必须去设置<应用程序和通知<特殊应用程序访问<通知<通知访问<为“像素启动器”打开。然后通知点将按预期出现!
我想是时候写报告了。