首页 > 解决方案 > 在已删除通知倾斜的应用中获取重复通知

问题描述

在调试我的应用程序时,我得到了一个奇怪的输出。首先,当我运行应用程序时,警报时的通知标题是按照下面的代码,即 Dosing Alert!:

    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 条通知。一个标题为“剂量警报!” 另一个标题为“Dosing Alert”(缺少感叹号。这个标题是我一开始设置的,但后来我添加了感叹号)。我到处搜索这个问题,但不明白为什么会这样。

这是我的通知助手类。

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


}

}

请帮我解决。

标签: androidandroid-notifications

解决方案


推荐阅读