在调试我的应用程序时,我得到一个奇怪的输出。首先,当我运行该应用程序时,警报时的通知标题与下面的代码相同,即剂量警报!
String banner="Time to dose your "+AType;
NotificationHelper notificationHelper=new NotificationHelper(this);
NotificationCompat.Builder
nb=notificationHelper.getChannel1Notification("Dosing Alert!",banner);
notificationHelper.getManager().notify(1,nb.build());
但是在那之后我收到了2条通知。标题为“剂量警告”的一个。另一个标题为“剂量警告”(感叹号丢失。这个标题最初是由我设置的,但后来我添加了一个感叹号)。我到处搜索了此问题,但不明白为什么会这样。
这是我的通知帮助程序类。
public class NotificationHelper extends ContextWrapper {
public static final String channel1ID="channel1ID";
public static final String channel1IName="Just Plant Them";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
@TargetApi(Build.VERSION_CODES.O)
public void createChannels() {
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1IName, NotificationManager.IMPORTANCE_HIGH);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager() {
if (mManager==null){
mManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification(String title,String message){
return new NotificationCompat.Builder(getApplicationContext(),channel1ID)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSmallIcon(R.drawable.plant)
.setPriority(NotificationCompat.PRIORITY_MAX);
}
}
请帮助我解决问题。