• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Alarm manager not working properly for scheduled notifications.

Shivang_111

Lurker
Aug 29, 2017
1
0
I am making an application in which when user checks daily radio button it gets daily notification at certain time only once and if user checks hourly radio button it should get a notification every hour until it cancels the alarm but my code is not working i get random notifications when i set it to daily and hourly

Plus when i restart my device the alarm goes off too and user didnt get any notifications which were set previously evem though i used BOOT_RECEIVE_COMPLETE.

My button click event :-

Code:
Calendar calender = Calendar.getInstance();
                Intent intent = new Intent(getApplicationContext(), notification_receiver.class);
                 PendingIntent pendingintent = PendingIntent.getBroadcast(Main2Activity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                if (r1.isChecked()) {

                    calender.set(Calendar.HOUR_OF_DAY, 18); // For 1 PM or 2 PM
                    calender.set(Calendar.MINUTE, 30);
                    calender.set(Calendar.SECOND, 0);
                    alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,calender.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingintent);
                    Toast.makeText(Main2Activity.this, "Affirmations Scheduled", Toast.LENGTH_SHORT).show();
                }

                if (r2.isChecked()) {

                    alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
                            calender.getTimeInMillis(),
                            1000 * 60 * 2, pendingintent);
                    Toast.makeText(Main2Activity.this, "Affirmations Scheduled", Toast.LENGTH_SHORT).show();
}
});

My bradcast receiver class:-

Code:
public class notification_receiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent repeating_activity=new Intent(context,activitiesToStart.get(pos2));
        repeating_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(context,100,repeating_activity,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentTitle("Affirmation")
                .setContentText(notificationTexts.get(pos))
                .setAutoCancel(true)
                .setVibrate(new long[]{1000,1000,1000,1000,1000})
                .setLights(Color.RED,3000,3000);
                notificationManager.notify(100,builder.build());
    }
}

Please help i tried everything its not working properly..thanks.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones