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

Android SQLite wrapper keeps query open in CursorAdapter, is this good?
Android get RAW_CONTACT image uri
HTTP FAILED: java.net.SocketException: Socket closed; doesn't fire up exception handling methods
how to copy one arrayList to another without refrencing first?
detail pan is cut when master pan is opened in slidingpanlayout
Show image instead of circle on LineChart
how to use strace on windows operating system
Unable to send android.intent.action.BOOT_COMPLETED from adb shell
kernel error during compiling Cosmic-os[aosp]
Can Apache Cordova generate certificates?
FirebaseMessagingService not called after second background
Graphview issue in Lower versions
How to obtain code coverage from manual testing on android application (Android studio and gradle)
MqttConnection throws exception(Failed to ensure directory) when trying to connect
Make PC set up call between headset and phone
Android : Offline authentication in android without sms

Categories

HOME
batch-file
notepad++
fuelux
cheerio
linkedin
mjml
jmx
rsa
yahoo-finance
camera
owl-carousel
database-normalization
cq5
parameters
regular-language
babeljs
maxscript
frontend
teechart
spin
windows-ce
alamofire
opengl-es-3.0
angular-dart
docx
libusb
linear-algebra
esoteric-languages
xmonad
nodemcu
visual-glitch
undertow
dpdk
postscript
jboss6.x
maze
opentk
corpus
progress-bar
iis-express
ntp
card.io
fossil
watchman
xdocreport
nohup
projects-and-solutions
setup.py
mcustomscrollbar
odoo-website
apriori
double-click
jquery-ui-draggable
reportservice2010
openblas
x-ray
boxing
mouseleave
py2neo
objectscript
rmongo
slackware
stax
preprocessor
avspeechsynthesizer
udpclient
refinerycms
nservicebus5
lz77
nupic
concept-insights
zen-cart
extjs-grid
coda
jsvc
cwrsync
rrule
adehabitathr
theos
android-cookiemanager
wcm
human-computer-interface
tweenlite
strawberry-perl
encryption-asymmetric
ora-00904
android-framework
syndicationfeed
visual-studio-express
dtmf
virtual-pc
robotlegs
mkv
jquery-address
fsevents
pylucene
maven-1
drupal-fivestar
spread
cuda.net
fault-tolerance
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