I'm still not very good in Android dev, and I'm trying to figure out how can I do trigger some action (in my case I want to save a variable in shared preferences) after I click a Notification in Android.
I currently have this code:
private void startForegroundService() {
Log.d(TAG, "Start " + NOTIFICATION_CHANNEL_NAME + " foreground service");
setNotificationLanguage();
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(getResourceIdForResourceName(this, "ic_stat_show_chart"))
.setSound(null)
.setContentTitle(NOTIFICATION_CHANNEL_NAME)
.setContentText(NOTIFICATION_CHANNEL_DESC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
}
When I click the notification I launch my MainActivity class, which is something that I also want. But I would also like to save a string in Shared Preferences in the notification is clicked. I already searched and found nothing. If someone could help I would appreciate it. Thank you :)