【发布时间】:2023-04-01 11:26:01
【问题描述】:
这是祈祷时间本地通知。当它与祈祷时间相匹配时,我们会收到通知。 问题是,它总是迟到几分钟。 所以要克服想要提前一分钟通知。
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);
我应该做些什么改变。
【问题讨论】:
-
不知道你为什么打电话给
prayerTimes.get(0);,我想应该是prayerTimes.get(i);。 -
我有一个祷告列表。我有 5 集 5 祈祷。所以我正在使用 for 循环。 for (int i=0; i
-
@HB。嘿,我已经编辑了这个问题以便更好地理解。
-
String time = prayerTimes.get(0);的值是多少? -
@HB 字符串时间值 = 祈祷时间.get(0);在这种情况下是祈祷的时间是早上 5:44
标签: java android notifications localnotification