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

Spinner down arrow disappears below view background
Why app is not crashing on 6.0 (API23) platform for deprecated getResources().getColor() without support library?
How do I replicate a website's JSON Request in my Android App
Finding a point along a geodesic polyline in android maps api
Galaxy S6 GPS sample delayed after phone idle for extended period
Record texture any frame in video file
java.lang.IllegalStateException: Fragment already added:
Wordpress REST API link and first image from videos from YT
Zxing ViewFinderView view updates,but scanning area not updated on Nexus 5X
Android Horizontal View to Contain a List Starting from Center
Best way to get user GPS location in background or foreground in Android
How to run correctly SurfaceView in Fragment?
How to place a image using imageview below tabbed bar in android?
Google Play Store Commission fees
calculate app uptime - android
I am trying to use camera by reading android developer documentation and i am getting null as the intent data

Categories

HOME
gdb
flex
obfuscation
requirements
devise
knitr
square
reactive-programming
coin3d
linear-algebra
esoteric-languages
dreamweaver
git-extensions
topology
charles
pagespeed
postscript
joptionpane
object-storage
jquery-callback
strongname
dapper-extensions
slidetoggle
opentk
asymptotic-complexity
gp
corenlp-server
livelink
anki
powershell-ise
jmockit
jbutton
reactjs.net
mnist
processwire
apiary.io
google-earth-engine
health-monitoring
laravel-elixir
datacachefactory
parser-generator
hammer.js
zenhub
boo
bbpress
webjars
microstation
x-tag
jboss-tools
respondcms
refinerycms
lz77
rasterize
sapi
opl
dock
canalyzer
viola-jones
jquery-forms-plugin
cakephp-2.7
phpldapadmin
grouping-sets
webmatrix-3
crowd
tweenlite
mobility
ignite-ui
ss7
ray
buffering
websphere-esb
multitasking
http-patch
dolby-audio-api
mantle
stacky
robotlegs
boost-msm
objectquery
landscape-portrait
cgbitmapcontextcreate
timthumb
inline-formset
commerceserver2007
load-time
easyb
lalr
cldc
pbcopy

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile