Alarm Notification fires instantly. Android

后端 未结 5 2015
忘掉有多难
忘掉有多难 2021-02-15 18:15

I am working on a Reminder that sends notification on fixed time to the user.

The alarm is getting off instantly ...

I tried most of the suggestions over s

5条回答
  •  爱一瞬间的悲伤
    2021-02-15 18:54

    Got hit by same issue and stumbled upon this question while finding the solution. When setting the alarm, You just need to place a check that your alarm date should not be before current date.

    public static void setReminder(Context context,Class cls,long milliseconds, int event_id,String eventName)
    {
        Calendar calendar = Calendar.getInstance();
    
        Calendar notificationcalendar = Calendar.getInstance();
    
        notificationcalendar.setTimeInMillis(milliseconds);
    
        if(!notificationcalendar.before(calendar)) { // **just add this check**
    
    
            ComponentName receiver = new ComponentName(context, cls);
            PackageManager pm = context.getPackageManager();
    
            pm.setComponentEnabledSetting(receiver,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
    
    
            Intent intent1 = new Intent(context, cls);
            intent1.putExtra("eventName", eventName);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, event_id, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, notificationcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    
    }
    

提交回复
热议问题