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 not showing selected or default choice
how to use adobe creative sdk in NDK based project
Getting access to the currently shown fragment in fragmentpageradapter
Search using android search view widget not showing correct position after searching
My Bluetooth app is not scanning the nearby devices in Nexus 5X
Android how to add an expandable listView for navigation drawer item?
Get Last Updated Time Of Contacts In Android programmatically
Upload Image on Server Without converting into BITMAP
How to toggle a view from normal screen and full screen and vise versa in android
how to make app launch from any screen in android?
Fragment calls the same network call again
Make a layout that arrange its child in circular fashion Android
How can I put a marker on the top when using mapbox on android
Required XML attribute 'adSize' was missing [duplicate]
TableLayout resizes when TableRow is full
How to Bind an Activity to Bound Services Defined in Other Assemblies?

Categories

HOME
twitter-bootstrap-3
fpga
gpu
sublimetext
entity-framework-core
alsa
telegram-bot
x264
android-json
crystal-reports-2013
jax-ws
google-schemas
opengl-es-3.0
cmdb
gkturnbasedmatch
symfony-2.8
asp.net-mvc-viewmodel
google-container-registry
apache2.4
ag
exit
highstock
flooding
fragment-backstack
asymptotic-complexity
amazon-quicksight
winexe
sinch
ceylon
bioinformatics
vex
postback
complex-numbers
cognos-bi
cart
git-squash
clickjacking
dynamic-jasper
hibernate-search
jwplayer6
yowsup
searchkit
xv6
stringtokenizer
branching-and-merging
escpos
optimizely
servermanager
fwrite
nanoc
jquery-ui-draggable
hammer.js
google-cse
css-transforms
offline.js
choice
visual-studio-2008-sp1
model-associations
aurora
lz77
database-relations
ascii-art
file-move
video-player
toolkit
sapscript
slash
evolus-pencil
phpldapadmin
filesplitting
easyrtc
formview
insight.database
notorm
sphinxql
asdf
android-icons
mantle
qi4j
veracity
os.system
apache-whirr
jquery-address
google-ajax-api
massive
socketserver
folding
zend-rest
blender-2.49
murmurhash
filesystemobject
int64

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