java – Android 7.1 and lower double notifications showing-ThrowExceptions

Exception or error:

I added scheduled notifications in my app and everything works well except for android version less than 7.1 where double notifications are showing.

Here is my notification publisher class (BroadcastReciever)

public class NotificationPublisher extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannels();
    }

    sendNotification("title", "body", notificationId, activity);
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
    channel.enableLights(true);
    channel.enableVibration(true);
    channel.setDescription(CHANNEL_DESCRIPTION);
    channel.setLightColor(Color.RED);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);
}

public void sendNotification(String title, String body, int id, Class activity) {
    Intent intents = new Intent(context, activity);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intents, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).bigText(body))
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build();

    NotificationManagerCompat.from(context).notify(new Random().nextInt(), notification);
}

}

Here is my code to schedule the notification:

public static void createNotification(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("app.action.DISPLAY_NOTIFICATION");
    intent.putExtra("notificationId", notificationID);

    Calendar calReminder = Calendar.getInstance();
    calReminder.setTimeInMillis(System.currentTimeMillis());
    calReminder.set(Calendar.HOUR_OF_DAY, 16);
    calReminder.set(Calendar.MINUTE, 30);

    PendingIntent broadcast = PendingIntent.getBroadcast(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calReminder.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}

Here is my MainActivity code at app startup:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private NotificationPublisher broadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    broadcastReceiver = new NotificationPublisher();

    setNotificationScheduler(this);
}

public static void setNotificationScheduler(Context context) {
    AppNotifications.createNotification(context);
}

@Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter("app.action.DISPLAY_NOTIFICATION");
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    registerReceiver(broadcastReceiver, intentFilter);
}

}

I can’t find why it is showing double notifications on android 7.1 and lower.

Also just to make sure: is creating notifications on startup wrong? is there a more by the books way to do it?

Thanks in advance.

How to solve:
 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app);
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

Add This Code To Your notification publisher class.

Leave a Reply

Your email address will not be published. Required fields are marked *