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

How to shuffle a list of activities
SharedPreferences not saving values properly
How to show 3 dots at the end of text in textview
Android JsonArray Parse
Unable to restore the theme last set by user?
android: Installation Error:Unknown Failure-while running an apk file in
ListView footer doesn't appear UNTIL the second time it is added
NetworkOnMainThreadException on Facebook Login
In Android how to get the list of files in sys folder
Exception when calling database functions from anothe class
Project Contains Error but not showing in source code Android
Build a service for XMPP - ANDROID
Android performance analysis and monitoring tool
Pass html format through email message body in android using spaned (only last date passed)
Understanding ArrayAdapter used for Listview
App migration on Google play, new package for same app

Categories

HOME
notepad++
google-cloud-platform
jax-rs
pyqt
camera
mstest
tail
spring-session
retrofit2
nstableheaderview
minecraft-forge
watson-conversation
3nf
parameter-passing
powershell-v5.0
chargify
template-engine
google-picker
mapstruct
bourbon
swagger-2.0
solrcloud
spring-async
babel
r-mice
roundup
onload
progress-bar
linked-data
sqlpackage
vex
alm
eviews
zend-debugger
android-maps-extensions
hibernate-search
modalviewcontroller
sharepoint-workflow
dm-script
password-protection
demo
metawidget
unordered-map
signals-slots
bindingnavigator
show-hide
procfile
optimizely
ovf
openal
proget
linter
qtranslate
yii2-user
backbone-collections
sandcastle
smartfoxserver
stax
redistributable
greatest-n-per-group
deep
html-to-pdf
ellucian-scribe
nullable
kendo-upload
jaunt-api
toran-proxy
oracle-spatial
rubaxa-sortable
sapscript
skspritenode
canvg
mraid
bufferedinputstream
ssis-data-flow
genymotion-call
create.js
matrix-inverse
getopt
kendo-dataviz
cocoalibspotify-2.0
cascadingdropdown
booksleeve
device-width
panda3d
appjs
luabind
user-tracking
exponentiation
tournament
mbeans
quotation-marks
cdonts
load-time
uideviceorientation
easyb
office-2007
zend-rest
cfwindow

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