简体   繁体   English

Android 工作室通知

[英]Android studio notifications

I have a problem with notifications in my app.我的应用程序中的通知有问题。 I want to set multiple notifications for different hours of the day.我想为一天中的不同时间设置多个通知。 For example, let's take 8, 12 and 23 o'clock.例如,让我们以 8 点、12 点和 23 点为例。 But only the one at 23 o'clock triggers every day.但每天只有23点触发。 What's wrong with my code and will it work even if the app is killed?我的代码有什么问题,即使应用程序被杀死,它还能工作吗? Here's the code that sets alarms in my activity这是在我的活动中设置警报的代码

public void myAlarm(int hour, int minute) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);


    Intent intent = new Intent(getApplicationContext(), Reminder.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    if (alarmManager != null) {
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

and this is what I wrote in onCreate这就是我在 onCreate 中写的

        myAlarm(8, 0);
        myAlarm(12, 0);
        myAlarm(23, 0);

this is my receiver这是我的接收器

public class Reminder extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    Intent in = new Intent(context, MySecoundActivity.class);
    in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "Reminder")
            .setSmallIcon(R.drawable.bell)
            .setContentTitle("Notification!")
            .setContentText("Text of notification")
            .setColor(0xfb3ff)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH);


    NotificationManagerCompat notificationMngr = NotificationManagerCompat.from(context);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Reminder";
        String description = "reminder channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel("Reminder", name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);

    }
    notificationMngr.notify(200, builder.build());
}

Receiver is in android manifest接收器在 android 清单中

<receiver android:name=".Reminder"/>

This is the expected behavior since in your code, when you set the alarm, you have to give every PendingIntent unique requestCode which in your case remains the same, ie, 0. So when you set another alarm with same requestCode, the last alarm gets updated to the new time instead of creating a new alarm.这是预期的行为,因为在您的代码中,当您设置警报时,您必须为每个 PendingIntent 提供唯一的 requestCode,在您的情况下保持不变,即 0。因此,当您使用相同的 requestCode 设置另一个警报时,最后一个警报会得到更新到新时间而不是创建新警报。 So only your last alarm works.所以只有你的最后一个闹钟有效。 So at the time of setting the alarm, in your PendingIntent.getBroadcast() , instead of the 0, you could use either the Random class to generate a random number every time or you could just use (int)System.currentTimeMillis() which would always be a different number.因此,在设置警报时,在PendingIntent.getBroadcast()中,而不是 0,您可以使用 Random class 每次生成一个随机数,或者您可以只使用(int)System.currentTimeMillis()哪个永远是一个不同的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM