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());