private void createNoteNotification(int noteId) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("id", noteId);
intent.putExtra("dateTime", notificationDateTime);
intent.putExtra("title", titleField.getText().toString());
intent.putExtra("text", noteTextField.getText().toString());
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, noteId, intent, 0);
alarmManager.setExact(AlarmManager.RTC, notificationDateTime, alarmIntent);
}
public static class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra("id", 0);
long dateTime = intent.getLongExtra("dateTime", 0);
String title = intent.getStringExtra("title");
String text = intent.getStringExtra("text");
Intent deleteIntent = new Intent(context, DeleteNoteService.class);
deleteIntent.putExtra("id", id);
deleteIntent.setAction(DeleteNoteService.ACTION_DELETE_NOTE);
PendingIntent deletePendingIntent = PendingIntent.getService(context, id, deleteIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setWhen(dateTime)
.addAction(R.drawable.ic_delete_white_24dp, context.getString(R.string.delete), deletePendingIntent);
((NotificationManager) context.getSystemService(NOTIFICATION_SERVICE)).notify(id, builder.build());
}
}