java - 提前几分钟安排本地通知
问题描述
这是祈祷时间本地通知。当它与祈祷时间相匹配时,我们会收到通知。问题是,它总是迟到几分钟。因此,要克服想要提前一分钟通知。
StartNotificationreciever.class
for (int i=0; i<prayerNames.size(); i++){
String prayerName = prayerNames.get(i);
if (prayerName.equals("Fajr")) {
String time = prayerTimes.get(0);
String[] array = time.split(":");
int hour = Integer.parseInt(array[0]);
String timeMinuit = array[1];
String[] array2 = timeMinuit.split(" ");
int minuit = Integer.parseInt(array2[0]);
String finalNotificationText = "View namaz time in " + locality + " location - " + prayerTimes.get(0);
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minuit);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
calendars.add(calSet);
notificationTextArrayList.add(finalNotificationText);
}
NotificationReciever.Class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);
if (mChannelFajr == null) {
mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
mChannelFajr.setDescription(notificationText);
mChannelFajr.enableVibration(true);
mChannelFajr.enableLights(true);
mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
mChannelFajr.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(mChannelFajr);
}
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ") // required
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name));
} else {
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ")
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name))
.setPriority(Notification.PRIORITY_HIGH);
}
notification = builder.build();
notificationManager.notify(0, notification);
这是生成通知的地方。
if (targetCal.getTimeInMillis()>System.currentTimeMillis()) {
int requestCode = (i) * 100;
Intent intent = new Intent(context, NotificationReceiver.class);
intent.setAction("com.shiamomin.sjmmj.MY_NOTIFICATION");
intent.putExtra("notificationText", finalNotificationText);
intent.putExtra("requestCode", requestCode);
intent.putExtra("notificationType", notificationType);
Log.d("notificationType", String.valueOf(notificationType));
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
requestCode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
mgrAlarm.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),
pendingIntent);
Log.d("time", String.valueOf(mgrAlarm));
Log.d("targetTime", String.valueOf(targetCal.getTime()));
intentArray.add(pendingIntent);
我应该做些什么改变。
解决方案
推荐阅读
- flutter - Flutter Moor 加入和在哪里
- bash - SQLITE3 从导入中排除 - 表中已经存在的数据行 - 不使用主键
- python - 部署到heroku(Django项目)后,Pandas数据框未按预期运行
- python - 如何使用 for 循环简化此字典创建
- javascript - 如何在异步 api 请求后重新路由我的反应应用程序
- javascript - 在矩阵的第 N 列中查找最大/最小元素索引的快速方法
- python - 在 Mac 上运行 Tkinter
- amazon-s3 - Sentry 不会将 DSYM 文件上传到 S3 文件存储后端并给我异常:NoSuchKey: An error occurred (NoSuchKey)
- swift - 如何在 iOS 13 上以编程方式更改顶部栏背景 swift 4
- jmeter - 如何发送 RFH2 格式的消息?