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

Arraylist<String> ArrayAdaptor to display 2 Arraylist<String> in one ListView
TextView becomes static
Color Picker UI for Android Wear
ScrollView event Overwrites DrawView Event
Android Application Navigation
How to call native function from cordova 3.x
Android: Prevent focus from jumping when hiding view
Create an apk on button click
How to use log4j Logger in Android Testing project
how to prevent other app to bind to my service
notification not firing on particular times in android
non-system libraries in linker flags
Android: items on ListView changes background color after click
Vuforia jPCT integration and build jni
How to mute downlink voice call on android?
Create and use Database in phoneGap is not working

Categories

HOME
cygwin
time-complexity
mstest
rendering
newrelic
tivoli
polling
mvc5
azure-container-service
opengl-3
modal-dialog
project
openstreetmap
ibeacon-android
heap
appcelerator-studio
plist
rtf
frequency-distribution
plink
heat
bitnami
altera
http-post
internationalization
null-pointer
shortest-path
dynamically-generated
perlbrew
mount
siri
nstimer
8051
iis-express
nsurl
boost-asio
angularjs-material
biztalk-rule-engine
python-import
reverse
roblox
swagger-php
apic
amazon-ses
laravel-elixir
dbnull
show-hide
vaadin-charts
uiimage
x++
mesosphere
nanoc
chai-as-promised
user-defined-fields
vugen
launchctl
bluez
headless
skin
py2neo
baidu
rocks
google-cloud-console
nikeplus-api
tooleap
jclouds
pre-build-event
phpgrid
computer-architecture
fuelcms
extjs-grid
doctype
slash
batik
eoferror
dronekit-android
zxspectrum
actionfilterattribute
phpldapadmin
infosphere-spl
at-job
webfinger
verold
mt4j
ebay-lms
tlf
cnf
workitem
diem
android-icons
booksleeve
rmiregistry
failing-tests
jsr286
google-maps-mobile
rmdir
umra
xui
twitterizer
web-garden
zen
echo3
skype4java
mathematical-notation
wordprocessingml
flvplayback
technical-debt
soappy
information-management

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