android


Displaying messages from multiple notifications -Android


I am using Firebase to send notifications to my App, when I receive the notification I store the message in my shared preferences and then display the message in an activity. However, as of right now I am only saving the latest notification message in the shared preferences. How do I save all the messages from the notifications in the shared preferences?
I hope that you have some unique id for the message you are receiving from firebase. Now when you save the message in the shared preference use that unique id from the payload as the key. In your case, the MESSAGE_KEY is the same that's why it gets overwritten with the latest one. Just make the MESSAGE_KEY unique in the saveKeyMessage function and new shared preference instance will be created for each notification you receive. OR You can use the following code to set the key as the unique key for you shared preference generation.
public boolean saveKeyMessage(String message){
SharedPreferences sharedPreferences =
mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Log.d("MainActivity", "Current Timestamp: " + format);
editor.putString(unique_message_id, message);
editor.apply();
return true;
}
You can use the following code to display all shared preference instances
SharedPreferences prefs =
mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
Map<String, ?> allPrefs = prefs.getAll(); //your sharedPreference
Set<String> set = allPrefs.keySet();
for(String s : set){
LOG.d(TAG, s + "<" + allPrefs.get(s).getClass().getSimpleName() +"> = "
+ allPrefs.get(s).toString());
Hope this solves you problem! Cheers!
Use HashSet to store messages into SharedPreferences.
#. Store messages into SharedPreferences:
public static final String KEY_MESSAGES = "messages";
............
.................
public boolean saveKeyMessage(String message) {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
// Get existing messages
Set<String> messages = getMessages();
// Add new message to existing messages
messages.add(message);
// Store messages to SharedPreferences
editor.putStringSet(KEY_MESSAGES, messages);
editor.apply();
return true;
}
#. Get messages from SharedPreferences:
public Set<String> getMessages() {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
// Get messages
Set<String> messages = sharedPreferences.getStringSet(KEY_MESSAGES, new HashSet<String>());
return messages;
}
#. Get last message from SharedPreferences:
public String getLastKeyMessage() {
ArrayList<String> messageList = new ArrayList<String>(getMessages());
return messageList.get(messageList.size() - 1);
}
Hope this will help~

Related Links

android XML file not recognized
Change style of first item in listview
Android Read File Names SDCard [duplicate]
Passing android Camera object from one activity to another
Parse.com query returning 0 results when in the Data browser I can see there are 2 results
Android - Scoreloop Andengine social media share
Android cache data and update strategy?
android handwriting to text
startActivityForResult from within a DialogInterface.OnClickListener
In android phonegap application screen UI does not renders it showing patch on keypad area when keypad hides back
I want to give user money using paypal
Navigation Drawer item background colour for selected item
How to implement date picker as Listview in android?
Android - Error: com.facebook.FacebookException: Failed to get app name
how to create properties file android
play adobe flash on a splash screen

Categories

HOME
google-app-engine
polymer
battery
replace
alarm
alsa
okhttp
startup
java.util.logging
resultset
redis-sentinel
nancy
flatpak
android-intent
mvc-mini-profiler
forgot-password
video-editing
hta
easeljs
mongoid6
gpio
motion-detection
tpm
tizen-tv
jaws
scrollview
dotspatial
deltaspike
laravel-eloquent
apiary.io
apic
sql-server-ce
gdata
odoo-website
ssrs-2014
placeholder
angular-http
optimizely
fwrite
dynamic-linq
xbmc
netbeans6.8
dtsearch
twilio-click-to-call
insight
static-cast
java-bytecode-asm
ngmaterial
boo
gpg-signature
flac
pybrain
gameplay-kit
baidu
donations
vcloud-director-rest-api
synonym
http-status-code-401
omniauth-facebook
lua-telegram-bot
uiactionsheet
libav
false-sharing
roadkill-wiki
forwarding
django-urls
page.js
dock
sapscript
cryptographichashfunction
grouping-sets
boost-program-options
mruby
jbake
ssis-data-flow
crowd
webfinger
genymotion-call
jqmodal
named-parameters
cuba
android-audiorecord
xmltodict
visual-studio-express
mbunit
merb
kaleidoscope
monotouch.dialog
correlated-subquery
ilasm
procedures
workflow-services
matlab-load
xcelsius
nhprof
load-time
resharper-6.0
tabpanel
nstokenfield
strtod
httpcookie
lemmatization

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App