0

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 :)

Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
Johny Boy
  • 99
  • 8

5 Answers5

1

Try doing below in your Main Activty:

Intent intent = getIntent();
if (intent.hasExtras()){
   //Saved notification has been clicked to shared preferences
}

Hope this might help

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
1

You can pass arguments to intent, and in MainActivity check is intent have arguments Passing arguments to intent android

Konrad Sikora
  • 11
  • 1
  • 3
1

Add String value which you want to save your sharedpref as extra in your notificationIntent, something like below.

notificationIntent.putExtra("Key","Value");

You will be able to extract it in your onCreate of your Activity.

 String value = getIntent().getStringArrayExtra("Key");

Note: If you just want to know if the activity is opened by notification, just pass any constant value in intent and check if you get that in the onCreate using the above approach, that way you are sure that the activity is launched by notification. Just make sure in that condition not to use the same key/value combination as extras for the same activity intent from launching it from anywhere else other than notification pendingIntent flow.

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
1

As Keshav explained, You can get the data from the intent and store it in the sharedpreferences, Here is SharedPreferenceHelper class.

public class SharedPreferenceHelper {

 private static final String STRING_NAME = "your_string_name";

 public SharedPreferenceHelper(Context context) {
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
  }

 public void setString(String your_string){
    mPreferences.edit().putString(STRING_NAME,your_string).apply();
}

public String getString(){
    return mPreferences.getString(STRING_NAME,null);
}

}

In the MainActivity,

public class MainActivity extends AppCompatActivity{

 Bundle mBundle;
 SharedPreferenceHelper mSharedPreferenceHelper;

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

    mSharedPreferenceHelper = new SharedPreferenceHelper(this);
    mBundle = getIntent().getExtras();

    if(mBundle != null){
     mSharedPreferenceHelper.setString(mBundle.getString("your_string_data"));
     }

}

If activity is already created then,

@Override
protected void onResume() {
    super.onResume();
  if(mBundle != null){
     mSharedPreferenceHelper.setString(mBundle.getString("your_string_data"));
     }
}

enter code here

Let me Know if you have any issues.

Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
1

Override method onNewIntent in your MainActivity

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("KEY", "str value"); //change this according to your need
    editor.apply();
}
Intra
  • 46
  • 1
  • 4
  • You are completely right. Its better to use onNewIntent, since the onCreate method only triggers if the MainActivity is not already running. Thank you very much!! – Johny Boy May 31 '21 at 15:54