繁体   English   中英

在一天中的不同时间显示本地通知 android?

[英]Display local notifications on different times of the day android?

我必须在一天中的不同时间显示本地通知并每天重复它们。 通知时间来自 API 格式我在 nottime 数组中我正在使用警报每天重复通知

以下是我的警报管理器代码

Intent intent = new Intent(this, DietNotificationReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
    String[] notTime=new String[]{"6:00 AM","8:00 AM","11:00 AM","1:00 PM","3:00 PM","6:00 PM","8:00 PM","10:00 PM"};
    for (int i = 0; i < pregnancyDiets.size(); i++) {
        intent.putExtra("title", pregnancyDiets.get(i).getSchedule());
        intent.putExtra("message", pregnancyDiets.get(i).getDescription());
        intent.putExtra("count", i + 10);
        try {
            Date mDate = sdf.parse(notTime[i]);
            long timeInMilliseconds = mDate.getTime();
            long timeInterval = 24 * 60 * 60  * 1_000L;
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilliseconds, timeInterval, pendingIntent);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

以下是通知的接收者

public class DietNotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    int notifyID = 1;
    String CHANNEL_ID = "my_channel_01";// The id of the channel.
    CharSequence name = "TBH";// The user-visible name of the channel.
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    Notification notification = new Notification.Builder(context)
            .setContentTitle(intent.getStringExtra("title"))
            .setContentText(intent.getStringExtra("message"))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setChannelId(CHANNEL_ID)
            .build();
    NotificationManager mNotificationManager =
            (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.createNotificationChannel(mChannel);
    mNotificationManager.notify(intent.getIntExtra("count",0) , notification);

}
}

警报管理器根本没有启动。 这里有什么问题?

暂无
暂无

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

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