1. Download our Official Android App: Forums for Android!

Alarm manager not working properly for scheduled notifications.

Discussion in 'Android Development' started by Shivang_111, Aug 29, 2017.

  1. Shivang_111

    Shivang_111 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Aug 29, 2017

    Aug 29, 2017
    1
    0
    5
    Male
    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 (Text):
    1. Calendar calender = Calendar.getInstance();
    2.                 Intent intent = new Intent(getApplicationContext(), notification_receiver.class);
    3.                  PendingIntent pendingintent = PendingIntent.getBroadcast(Main2Activity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    4.  
    5.                 if (r1.isChecked()) {
    6.  
    7.                     calender.set(Calendar.HOUR_OF_DAY, 18); // For 1 PM or 2 PM
    8.                     calender.set(Calendar.MINUTE, 30);
    9.                     calender.set(Calendar.SECOND, 0);
    10.                     alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
    11.                     alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,calender.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingintent);
    12.                     Toast.makeText(Main2Activity.this, "Affirmations Scheduled", Toast.LENGTH_SHORT).show();
    13.                 }
    14.  
    15.                 if (r2.isChecked()) {
    16.  
    17.                     alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);
    18.                     alarmmanager.setRepeating(AlarmManager.RTC_WAKEUP,
    19.                             calender.getTimeInMillis(),
    20.                             1000 * 60 * 2, pendingintent);
    21.                     Toast.makeText(Main2Activity.this, "Affirmations Scheduled", Toast.LENGTH_SHORT).show();
    22. }
    23. });
    My bradcast receiver class:-

    Code (Text):
    1. public class notification_receiver extends BroadcastReceiver{
    2.     @Override
    3.     public void onReceive(Context context, Intent intent) {
    4.         NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    5.         Intent repeating_activity=new Intent(context,activitiesToStart.get(pos2));
    6.         repeating_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    7.         PendingIntent pendingIntent=PendingIntent.getActivity(context,100,repeating_activity,PendingIntent.FLAG_UPDATE_CURRENT);
    8.         NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
    9.                 .setContentIntent(pendingIntent)
    10.                 .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
    11.                 .setContentTitle("Affirmation")
    12.                 .setContentText(notificationTexts.get(pos))
    13.                 .setAutoCancel(true)
    14.                 .setVibrate(new long[]{1000,1000,1000,1000,1000})
    15.                 .setLights(Color.RED,3000,3000);
    16.                 notificationManager.notify(100,builder.build());
    17.     }
    18. }
    Please help i tried everything its not working properly..thanks.
     

    Advertisement

Share This Page

Loading...