How can i create multiple notifications?

Viewed 176

Each notification must have its own ID. I am passing two different IDs to intents. But only the second of the two is displayed. What am I doing wrong?

Here's my Fragment OnClick Code with Constants and notification functions:

public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
public void onClick(View view) {
            scheduleNotification(getNotification( "10 second delay" ) , 10000, 1 ) ;
            scheduleNotification(getNotification( "5 second delay" ) , 5000, 2 ) ;
            OpenThis(view);
        }

private void scheduleNotification (Notification notification , int delay, int NotifID) {
    Intent notificationIntent = new Intent( this.getActivity(), ReminderBroadcast.class ) ;
    notificationIntent.putExtra(ReminderBroadcast.NOTIFICATION_ID , NotifID ) ;
    notificationIntent.putExtra(ReminderBroadcast.NOTIFICATION , notification) ;
    PendingIntent pendingIntent = PendingIntent. getBroadcast ( this.getContext(), 0 , notificationIntent , PendingIntent. FLAG_UPDATE_CURRENT ) ;
    long futureInMillis = SystemClock. elapsedRealtime () + delay ;
    AlarmManager alarmManager = (AlarmManager) this.getContext().getSystemService(Context. ALARM_SERVICE ) ;
    assert alarmManager != null;
    alarmManager.set(AlarmManager. ELAPSED_REALTIME_WAKEUP , futureInMillis , pendingIntent) ;
}

private Notification getNotification (String content) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder( this.getContext(), default_notification_channel_id ) ;
    builder.setContentTitle( "Scheduled Notification" ) ;
    builder.setContentText(content) ;
    builder.setSmallIcon(R.drawable. ic_launcher_foreground ) ;
    builder.setAutoCancel( true ) ;
    builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
    return builder.build() ;
}

My BroadcastReceiver

public class ReminderBroadcast extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id" ;
public static String NOTIFICATION = "notification" ;

private static final String TAG = "myLogs";
@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
    Notification notification = intent.getParcelableExtra( NOTIFICATION ) ;
    if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
        int importance = NotificationManager. IMPORTANCE_HIGH ;
        NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
        assert notificationManager != null;
        notificationManager.createNotificationChannel(notificationChannel) ;
    }
    int id = intent.getIntExtra( NOTIFICATION_ID , 0 ) ;
    Log.d("TAG", String.valueOf(id));

    assert notificationManager != null;
    notificationManager.notify(id , notification) ;
}

}

0 Answers
Related