0

I am setting an alarm, that fires once a day in the device. The Alarm fires ok, but it needs some data to work properly. That data is sent this way:

AlarmManager am=(AlarmManager)SplashActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent msgIntent = new Intent(SplashActivity.this, AlarmReceiver.class);
msgIntent.putExtra("todaydate", today);
PendingIntent pIntent = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, msgIntent, Intent.FILL_IN_DATA);
am.setRepeating(AlarmManager.RTC_WAKEUP, today, AlarmManager.INTERVAL_DAY, pIntent);

Then, in the AlarmReceiver, inside onReceive():

public class AlarmReceiver extends BroadcastReceiver {

private DatabaseHelper dbHelper;
private int NOTIF_ALERTA_ID=666;
@Override
public void onReceive(Context context, Intent intent) {
    long todayDate=intent.getLongExtra("todaydate", 0L);

    if(dbHelper==null){
        dbHelper=new DatabaseHelper(context);
    }
    Cursor c=dbHelper.getNotas(context);
    if(c.moveToFirst()){
        long milisTomorrow=todayDate+86400000;
        do{
            long noteFecha=c.getLong(1);
            if(noteFecha>todayDate && noteFecha<milisTomorrow){                    
                launchNotification(c.getString(2), context);
            }
        }while (c.moveToNext());
    }
    c.close();
}

private void launchNotification(String texto, Context ctx) {


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(ctx)
                    .setSmallIcon(android.R.drawable.stat_sys_warning)
                    .setLargeIcon((((BitmapDrawable) ctx.getResources()
                            .getDrawable(R.mipmap.ic_launcher)).getBitmap()))
                    .setContentTitle(ctx.getString(R.string.notenoti))
                    .setContentText(texto)
                    .setTicker("Alert!");

    Intent notIntent =
            new Intent(ctx, CalendarioActivity.class);

    PendingIntent contIntent =
            PendingIntent.getActivity(
                    ctx, 0, notIntent, 0);
    mBuilder.setContentIntent(contIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(NOTIF_ALERTA_ID, mBuilder.build());
}

But this returns 0. What am I doing wrong?

Would like to note that I has set a Log entry in the onReceive method, and it is accessed.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

1 Answers1

0

Try to build PendingIntent in next way

PendingIntent pIntent = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);

I replaced Intent.FILL_IN_DATA to PendingIntent.FLAG_UPDATE_CURRENT

Before checking the code delete and install the application.

Ilya Tretyakov
  • 6,848
  • 3
  • 28
  • 45
  • I had that flag before changing it to Intent.FILL_IN_DATA, and it was not working. Changed it because of this question: http://stackoverflow.com/questions/5216177/sending-extras-to-broadcastreceiver – Fustigador Sep 14 '15 at 09:38
  • It is really interesting how FILL_IN_DATA could help. I think it is not correct to use this flag here. But, back to your problem. Try to add this line to the code msgIntent.setAction("actionstring" + System.currentTimeMillis()); – Ilya Tretyakov Sep 14 '15 at 09:47
  • I have ended up generating the value of the long (it comes from a Calendar object) in the receiver class, and that way the code works ok... – Fustigador Sep 14 '15 at 09:54