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

LocalBroadcastManager, android:process=“:ProcessName”, Too much ram usage
Get JSON in fragment one and save then show data in fragment tow
Working code for uploading mp3 to server on android
How should i reflect the incremented values of a textview ,in a custom listview,after the user restarts the application?
SwipeRefreshLayout in fragment bigger than the tab containing it
can any body please tell me how to create like this type of layout
Android eclipse dictionary code1
Creating a Circle ProgressBar
DisplayImageOptions configuration confict in universal image loader
recyclerView does not call the onBindViewHolder when scroll in the view
Roll around non-geomagnetical axis
StartActivityForResult with 2 childActivities
Focus and then switching to another scene
OpenGL Context Loss in Service
How to understand firstVisibleItem and visibleItemCount in onScroll method?
How to do with NullPointer exetpion in retrofit

Categories

HOME
shell
phpword
azure-stream-analytics
assembly
gpu
numbers
toolbox
graphics
devise
nstableheaderview
flume
recordrtc
accessibility
rtf
selenium-ide
mongodb-csharp
alpacajs
firefox-addon
wolframalpha
shortest-path
cfml
nim
r-mice
react-toolbox
highstock
jquery-callback
strongname
beautifier
licensing
rpgle
bioinformatics
failover
finagle
kudu
complex-numbers
nested-lists
roblox
apiary.io
watchman
filesystemwatcher
railstutorial.org
opensmpp
linqpad
x++
getchar
openal
lampp
term
robomongo
dbcontext
datamaps
aquamacs
spring-data-couchbase
libjpeg
java-websocket
ziparchive
jclouds
refinerycms
invoke-sqlcmd
aspen
state-restoration
custom-errors
microsoft-reporting
doctype
canalyzer
haskell-warp
liquid-layout
dnssec
aspectj-maven-plugin
jquery-forms-plugin
fits
bcache
nodelist
machine-instruction
psr-4
ignite-ui
mintty
grails-domain-class
mt4j
artemis
solace-mq
windows-phone-8-sdk
asdf
process-explorer
joomla-template
android-icons
web-notifications
extreme-programming
web-farm
wcf-configuration
usertype
fitbounds
filemerge
u2netdk
rmdir
w3c-geolocation
unattended-processing
facebook-iframe
eclipse-marketplace
cfwindow
mathematical-notation
sqlobject
unmanagedresources
contentpresenter
commercial-application

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