How to recover Extras from a PendingIntent (Android)?

0

I'm implementing Notifications in an app.

I'm using AlarmManager with a PendingIntent of an Intent with data (in Extras, some model objects) but, when I get them in another BroadcastReceiver class, those extras are not there.

Here is the code:

MainActivity

private void saveNotification(Tarefa tarefa) {
    Intent intent = new Intent(this, MyAlarmBroadcastReceiver.class);
    intent.putExtra("tarefa", tarefa); 
    // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - tentei usar essas flags, mas não funcionou
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
    Toast.makeText(this, "setting the notification in 5 seconds", Toast.LENGTH_SHORT).show();
}

MyAlarmBroadcastReceiver:

public class MyAlarmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras(); 
        Tarefa tarefa = (Tarefa) b.getSerializable("tarefa");
        startNotification(context, taskTitle , taskText );
    }
}

My guess is that when I get an attempt in the onReceive method, this intent is not necessarily what was previously sent in the saveNotification method, my question is how to retrieve this data from the right Intent.

EDIT

I'm trying to send a Tarefa object that implements the Serializable class, but the object returns null by throwing the NullPointerException. Fixed the above code.

They asked about the AndroidManifest.xml if I had included a receiver, it follows code:

<receiver android:name=".receiver.MyAlarmBroadcastReceiver ">
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver> 

Thanks in advance.

    
asked by anonymous 05.04.2017 / 16:07

0 answers