Kieran Snapes
Kieran Snapes

Reputation: 23

Alarm manager not triggering

I'm new to this, so I'm a bit lost.

I have built a notification and passed it to the alarm manager as a pendingIntent, however instead of waiting the interval to trigger the alarm and showing the notification, the notification is instantly shown. What have I done wrong, that isn't allowing the alarm to be set properly?

public class NotificationController{

Context context;

public void createNotification(Context context){

    this.context = context;
    Notification notification = getNotification();

    //creates notification
    Intent intent = new Intent(context, NotificationReceiver.class);
    intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
    intent.putExtra(NotificationReceiver.NOTIFICATION, notification);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    //schedules notification to be fired.
    AlarmManager  alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);

}


private Notification getNotification(){

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Notification.Builder builder = new Notification.Builder(context)
            .setContentTitle("Reminder")
            .setContentText("Car service due in 2 weeks")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
    return builder.build();
}
}

my receiver

public class NotificationReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(id, notification);

}
}

I have also registered with <receiver android:name=".NotificationReceiver" > in the AndroidManifest.

Upvotes: 2

Views: 3230

Answers (4)

Abhijith mogaveera
Abhijith mogaveera

Reputation: 1304

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

and

Intent notifyIntent = new Intent(this, MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, NotificationKeys.NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar alarmStartTime = Calendar.getInstance();
        alarmStartTime.set(Calendar.SECOND, 5);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,  alarmStartTime.getTimeInMillis(),  pendingIntent);

If you want to set is at a excat time, you should use setExact. Unfortunalety there is no setExactRepating so you have to create this yourself. Schedule a new alarm after one executes or something like that

for more refer https://stackoverflow.com/a/30812993/8370216

Upvotes: 0

Arshad
Arshad

Reputation: 965

For me old methods are killing after few hours . Please see my code that i am using for different OS versions. May be helpful.

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(alarmTime, pendingIntent);
        alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
    }
    else {

        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
    } 

Thanks Arshad

Upvotes: 1

MarchingHome
MarchingHome

Reputation: 1204

In this line

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);

You specify that the alarm has to fire at 10 seconds after the device has been booted (which is in the past, so the alarm fires immediately). If you would want it 10 seconds after you set the alarm, you use the number of milliseconds since the device has been booted PLUS 10 seconds:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + 10000 , pIntent);

Upvotes: 4

KR_Android
KR_Android

Reputation: 1159

Try this:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis()+10000 , pIntent);

Upvotes: 0

Related Questions