每天重复通知,但每次都有随机内容

Making a daily repeating notification but with randomized content each time

我在这里尝试做的是在每次弹出通知时随机化通知的内容。但我不知道如何实现它,这是我当前的代码:

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

这是我的接收器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());
}
}

我的问题是在这个底部...

我怎样才能让它在每次警报触发时随机出现?

使用 Random class 生成随机对象(布尔值、整数等),然后 swtich 生成的数字并通过适当的通知进行通知。

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

我真的建议重构您的代码,因为无需创建 3 个具有许多共同属性的相同对象,您可以只使用一个对象并根据从随机生成器获得的情况更改其内容。下面是一个例子

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