neverStop
neverStop

Reputation: 43

How to open activity that corresponding to the position click when I clicked on the notification when pops up?

I have RecyclerView that contains notes, when I add new note I want to add reminder for that particular note but when the notification appears and I click on it I want to navigate to that activity and open that note. Notification works properly but what I want is, when I clicked on the notification open that note I set reminder on it.

AlarmReceiver class:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, ChecklistChildActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarmChannel")
            .setContentTitle("title")
            .setContentText("text")
            .setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setSmallIcon(R.drawable.alarm)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
    managerCompat.notify(123, builder.build());

}

}

setAlarm method:

 private void setAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, hour);
    calendar.set(Calendar.MINUTE, minute);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}

I have an o'clock interface in RecyclerAdapter that I implemented in MainActivity:

@Override public void onNoteClicked(int position, View itemView) {

        Intent intent = new Intent(this, NoteDetail.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("id", noteAdapter.itemList.get(position).getId());
        intent.putExtra("title", noteAdapter.itemList.get(position).getTitle());
        intent.putExtra("content", noteAdapter.itemList.get(position).getContent());
        intent.putExtra("date", noteAdapter.itemList.get(position).getDate());
        intent.putExtra("backgroundColor", noteAdapter.itemList.get(position).getBackgroundColor());
      
        startActivity(intent);
    }

When I clicked on the notification this method somehow should be called. When I add the note I don't know which position it goes in order to pass that position with the intent, though if I know the position, the position may change when I add or remove another note.

Upvotes: 0

Views: 59

Answers (1)

Abolfazl Abbasi
Abolfazl Abbasi

Reputation: 353

Great, I have an idea, I assume your activity gets id and other data from intent. so we have to put data to alarm intent and read them from AlarmReceiver and put them again into the Activity Intent.

 private void setAlarm() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, hour);
    calendar.set(Calendar.MINUTE, minute);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("id", "myId");
    intent.putExtra("title", "my title");

    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}

and get that from the Receiver class:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context, ChecklistChildActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("id", intent.getStringExtra("id"));
    i.putExtra("title", intent.getStringExtra("title"));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "alarmChannel")
        .setContentTitle("title")
        .setContentText("text")
        .setAutoCancel(true)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setSmallIcon(R.drawable.alarm)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
    managerCompat.notify(123, builder.build());

}

that is a simple way but I suggest using the database in your activity and getting the note details in the onCreate method with id, so you need just add id in your intents and add get query in NoteDetail.java class.

Upvotes: 1

Related Questions