alvin valdez
alvin valdez

Reputation: 161

Getting data from clicked notification in android

Hey guys I need help on how to get the data from my pending intent which is set using a broadcast receiver. What I want to happen is to get the data of an id when the notification is clicked which will be needed for my activity.

this is how I make the extras

public class AlertReceiver extends BroadcastReceiver {

    private int id;
    // Called when a broadcast is made targeting this class
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        String time = bundle.getString("time");
        id = bundle.getInt("id");

        createNotification(context, title, time, "Pharoah Reminder");
    }

    public void createNotification(Context context, String msg, String msgText,  String msgAlert){
        Intent reminderActivity =  new Intent(context, ReminderPreviewActivity.class);
        reminderActivity.putExtra("id", id);

        PendingIntent notificationIntent = PendingIntent.getActivity(context, id,
                reminderActivity, PendingIntent.FLAG_UPDATE_CURRENT );

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText)
                .setContentIntent(notificationIntent);

        mBuilder.setContentIntent(notificationIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

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

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

but then it is always null when I try to open my activity from the notification.

here how I get it.

public class ReminderPreviewActivity extends AppCompatActivity {

    private Toolbar mToolBar;

    private TextView titleTextView;
    private TextView descTextView;
    private TextView timeTextView;
    private TextView dateTextView;

    private String title;
    private String desc;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_preview);

        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);



        titleTextView   = (TextView) findViewById(R.id.titleTextView);
        descTextView    = (TextView) findViewById(R.id.descTextView);
        timeTextView    = (TextView) findViewById(R.id.timeTextView);
        dateTextView    = (TextView) findViewById(R.id.dateTextView);

        Intent extras = getIntent();

        if(extras.getStringExtra("title") != null){
            setContentFromExtras(extras);
        }else{
            setContentFromDB(extras);
        }
    }

    private void setContentFromExtras(Intent extras){
        title   = extras.getStringExtra("title");
        desc    = extras.getStringExtra("desc");
        date    = extras.getStringExtra("date");

        String[] dateDB = date.split(" ");

        titleTextView.setText(title);
        descTextView.setText(desc);
        timeTextView.setText(formatTime(dateDB[1])+" "+dateDB[2]);
        dateTextView.setText(formatDate(dateDB[0]));
    }

    public void setContentFromDB(Intent extras){
        String id = extras.getStringExtra("id");

        int reminderID = Integer.parseInt(id);

        titleTextView.setText(reminderID);
    }

I need the id to retrieve data from database. Same thing happens when I close the app.

Upvotes: 2

Views: 4226

Answers (1)

Bö macht Blau
Bö macht Blau

Reputation: 13009

In your AlertReceiver, you have declared

private int id;

and you use this int value in

reminderActivity.putExtra("id", id);

So you also have to get it as an int in your setContentFromDB() method:

int reminderID = extras.getIntExtra("id", someInt);
titleTextView.setText("" + reminderID);

where 'someInt' should be an int value which is normally never used or a default value if that makes sense in your case.

You got null from getStringExtra("id") because that's the return value if no String with key "id" was found.

And if you use an int value with TextView.setText(), it will be interpreted as a string resource. I think in your case ('id' is meant for database) that's bad.

Upvotes: 1

Related Questions