Search code examples
androidandroid-intentandroid-pendingintent

pendingIntent not sending extras, despite the flags


My pendingIntent is carrying no extras to the service, despite the flags I put in the intent or the pendingIntent itself.

DrawerActivity.class:

@Override
public void registerAlarmTask(Calendar time, String title, int iconId) {

    int requestId = (int) System.currentTimeMillis();

    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("title",title);
    intent.putExtra("icon", iconId);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.setAction("alarm");
    PendingIntent pIntent = PendingIntent.getBroadcast(this, requestId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

    Calendar newTime = Calendar.getInstance();
    newTime.set(Calendar.MINUTE, time.get(Calendar.MINUTE)-1);

    alarm.set(AlarmManager.RTC_WAKEUP, newTime.getTimeInMillis(), pIntent);
    Log.w(TAG, "Alarm created! " + newTime.toString());
}

And the StartCommand method from NotificationService:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
        Log.w(TAG, "Icon ID: " + intent.getIntExtra("icon", 404) + " / Title: " + intent.getStringExtra("title"));
        nBuilder.setSmallIcon(intent.getIntExtra("icon", R.drawable.ic_event_new));
        nBuilder.setContentTitle(intent.getStringExtra("title"));
        nBuilder.setContentText("A partida irá começar em instantes...");
        nBuilder.setTicker("Sua partida no Destiny está prestes a começar!");
        nBuilder.setAutoCancel(true);

        NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(0, nBuilder.build());

    Log.w(TAG, "Notification Service started!");
    Toast.makeText(this, "Notification Service started!", Toast.LENGTH_SHORT).show();

    return super.onStartCommand(intent, flags, startId);
}

The extras from the intent are always null, no matter the requestId, intent flags or pendingIntent flags i put.

Any tip in where I'm doing wrong? I've searched a lot, but found no answer.

Regards, Lucas Sene


Solution

  • Like Mike M. said in the comments, I just forgot to pass the Intent extra from the BroadcastReceiver to the Service. Yes, a silly mistake...

    Thanks for all comments!