首页 > 解决方案 > 提前几分钟安排本地通知

问题描述

这是祈祷时间本地通知。当它与祈祷时间相匹配时,我们会收到通知。问题是,它总是迟到几分钟。因此,要克服想要提前一分钟通知。

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

我应该做些什么改变。

标签: javaandroidnotificationslocalnotification

解决方案



推荐阅读