androidandroid-alarmsrepeatingalarm

Making a daily repeating notification but with randomized content each time


What i am trying to do here is to randomize the notification's content each time it pops up. But i have no idea how to implement that, here's what my current code looks like:

Main2Activity

public void startAlarm() {

    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent;
    PendingIntent pendingIntent;

    myIntent = new Intent(Main2Activity.this,AlarmNotificationReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);

    manager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+0,86400000,pendingIntent);
}

Here is my receiver class:

AlarmNotificationReceiver

public class AlarmNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context);
    NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context);
    NotificationCompat.Builder builder3 = new NotificationCompat.Builder(context);

    builder1.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Alarm 1 actived!")
            .setContentText("THIS IS MY ALARM")
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
            .setContentInfo("Info");

    builder2.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Alarm 2 actived!")
            .setContentText("THIS IS MY ALARM")
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
            .setContentInfo("Info");

    builder3.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Alarm 3 actived!")
            .setContentText("THIS IS MY ALARM")
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
            .setContentInfo("Info");

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1,builder1.build());
}
}

My problem here is in this bottom part...

enter image description here

How can I make it random each time the alarm triggers?


Solution

  • Use Random class for generating random objects (booleans, integers,... etc) then swtich the generated number and notify with the suitable notification.

    Random random = new Random(System.currentTimeMillis());
    switch(random.nextInt(3)){
        case 0:
            // notify builder 1
            break;
        case 1:
            // notify builder 2
            break;
        case 2:
            // notify builder 3
            break;
    }
    

    I really recommend refactoring your code as there is no need to create 3 identical objects with many common attributes where you can use only one and change its content according to the case you got from the random generator. Below is an example

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    
    builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
    
    Random random = new Random(System.currentTimeMillis());
    switch (random.nextInt(3)) {
        case 0:
            builder.setContentTitle("Alarm 1 activated!")
                    .setContentText("THIS IS MY ALARM")
                    .setContentInfo("Info");
            break;
        case 1:
            builder.setContentTitle("Alarm 2 activated!")
                    .setContentText("THIS IS MY ALARM")
                    .setContentInfo("Info");
            break;
        case 2:
            builder.setContentTitle("Alarm 3 activated!")
                    .setContentText("THIS IS MY ALARM")
                    .setContentInfo("Info");
            break;
    }
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());