3

I have a Service which can receive notifications (with Google Cloud Messaging) and notify the user. In that very service, I also store the message sent by Cloud Messaging by using SharedPreferences. I gather those messages in a HashSet, and neither that HashSet nor any of its elements are supposed to be deleted. That is important because one of my activities has to display that whole list of messages.

This is working fine except when the user happens to use the "Recent Apps" button to kill the application. When he does and then relaunch the app, some of the messages are not retrieved by the activity so I'm guessing some of them have been deleted somehow.

Am I not using SharedPreferences correctly? What am I supposed to do to avoid that? Here is my Service code: (the concerned part of my onHandleIntent method)

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Notifications.class), 0);
String message=extras.getString("message");

sharedpreferences=this.getSharedPreferences(Constantes.PREFERENCES,Context.MODE_PRIVATE);
Set<String> setMessages= sharedpreferences.getStringSet("SETMESSAGES", new HashSet<String>());
setMessages.add(message);
Editor editor = sharedpreferences.edit();
editor.putStringSet("SETMESSAGES", setMessages);
editor.commit();

//The notification:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon)
    .setContentTitle("New Notification")
    .setStyle(new NotificationCompat.BigTextStyle().bigText("You got some new message!"))
    .setContentText("You got some new message!")
    .setAutoCancel(true)
    .setDefaults(Notification.DEFAULT_SOUND);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Kritias
  • 187
  • 1
  • 12
  • 2
    Before editor.putStringSet("SETMESSAGES", setMessages); call the editor.clear(); – Ramesh Feb 14 '15 at 11:32
  • is above code you mentioned inside a 'static' method? If so, there are few know issues ,discussed in this link.http://stackoverflow.com/questions/11480871/sharedpreferences-being-reset-after-force-close – AADProgramming Feb 14 '15 at 11:33
  • @MeTechnical It's not in a static method, this code is whether in the onHandleIntent() method, or in a non-static method called by onHandleIntent(). (which signature is private void sendNotification(Bundle extras) – Kritias Feb 14 '15 at 11:52
  • @Ramesh It appears your solution works, thank you very much. How did you find out that solution? You can answer to my question if you want, I will select it as the correct answer. – Kritias Feb 14 '15 at 13:25
  • @Kritias Storing messages, in fact any data that changes frequently, in SP is not ideal and nor suggested. Rather you can use SQLite or Realm like Db for that. SPs are used to store small amount of info which changes occasionally and SP writing is a heavy operation. – Hanu Sep 14 '17 at 11:32
  • Possible duplicate of [Shared Preferences get lost after shutting down device or killing the app](https://stackoverflow.com/questions/9803838/shared-preferences-get-lost-after-shutting-down-device-or-killing-the-app) – Sam Oct 15 '17 at 05:16
  • See https://stackoverflow.com/a/31598304/238753 for the real solution to this problem. – Sam Oct 15 '17 at 05:16

3 Answers3

3

Just add editor.clear() before setting putStringSet. Like:

 sharedpreferences=this.getSharedPreferences
                (Constantes.PREFERENCES,Context.MODE_PRIVATE);
 Set<String> setMessages= sharedpreferences.getStringSet
                ("SETMESSAGES", new HashSet<String>());
 setMessages.add(message);
 Editor editor = sharedpreferences.edit();
 editor.clear();
 editor.putStringSet("SETMESSAGES", setMessages);
 editor.commit();
Ramesh
  • 526
  • 3
  • 14
0

I solved the issue thanks to Ramesh's comment. Adding editor.clear(); before editor.putStringSet("SETMESSAGES", setMessages); fixed it. However I don't have any idea about the reason why it didn't work before.

Kritias
  • 187
  • 1
  • 12
0

When you are updating a StringSet, either create a new copy of Set and update or remove the existing StringSet and then add in shared prefs

 String key = "SETMESSAGES";
 sharedpreferences=this.getSharedPreferences
                (Constantes.PREFERENCES,Context.MODE_PRIVATE);
 Set<String> setMessages= sharedpreferences.getStringSet
                (key, new HashSet<String>());
 setMessages.add(message);
 Editor editor = sharedpreferences.edit();
 editor.remove(key);
 editor.putStringSet(key, setMessages);
 editor.commit(); 

P.S. its better to call editor.apply() instead of editor.commit()

SKG
  • 774
  • 6
  • 7