dimanche 24 mai 2015

Notifications don't get fired - Android

I'm trying to make an an app that sends multiple scheduled alarms, shown through a notification according to a specific time (not time interval, but time) every day.

I already implemented the AlarmReceiver.class that fires the notifications at each intent, but the problem is that I don't get any notification. I tried to show a Log inside the onReceiver() method, but the message hasn't been shown, so the alarm hasn't been reached.

This is the setAlarm() function, that sets the hour and the time to the Calendar I instantiated:

public void setAlarm()
{
    SharedPreferences pref = this.getSharedPreferences("NOTIFICATION_CODE", Context.MODE_PRIVATE);

    String mName = NameFld.getText().toString();
    String mFormat = FormatSpn.getSelectedItem().toString();

    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    Calendar current = Calendar.getInstance();

    current.setTime(date);
    calendar.setTime(date);

    calendar.set(Calendar.HOUR_OF_DAY, picker_hour);
    calendar.set(Calendar.MINUTE, picker_minute);
    calendar.set(Calendar.SECOND, 0);


    if(current.before(calendar))
    {
        calendar.add(Calendar.DATE, 1);
    }

    medName = mName;
    medFormat = mFormat;

    int _reqCode = pref.getInt("reqCode", -1);
    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pIntent = PendingIntent.getBroadcast(this, _reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    aManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);

    editor = pref.edit();
    editor.putInt("reqCode", ++_reqCode);
    editor.commit();

}

To set the time, I implemented a TimePickerDialog and I stored hour and minute values to picker_hour and picker_minute. Since I have to send multiple alarms, I use SharedPreferences to store the reqCode used then to identify the PendingIntent every time one is sent.

This is the AlarmReceiver.class that fire a notification for each intent received. Every notification is identified through the same reqCode of the PendingIntent:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    String[] data = AddMedicine.getData();

    SharedPreferences pref = context.getSharedPreferences("NOTIFICATION_CODE", context.MODE_PRIVATE);
    int reqCode = pref.getInt("reqCode", -1);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setAutoCancel(true);
    builder.setTicker("It's pill time!");
    builder.setContentTitle(data[0]);
    builder.setContentText(data[1]);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Notification notification = builder.build();
    NotificationManager nManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    nManager.notify(reqCode, notification);
    Log.i("NOTIFICATION", "FIRED!");
}}

P.s. The alarm is set every time the user creates it, in order to schedule them through the reqCode.

So, anyone knows how to send multiple alarms according by time? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire