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 detect that a user has clicked the notufication and then restart a timer??? android
How to delete everything in a table sql
dpi for downloadable images
Android setEGLContextClientVersion() is not registering properly
android button and spinner are not working
Android app force close
Animate to User Location (Android)
Fragments based menu
“Ə” character in android
How to access and manipulate views inside a ListView item?
Why can't I get the URI of profile pic from a contact number?
Android - retrieve string array from resources
Android emulator crash on Mac
Position of row in ListView that checkBox is selected
Big bitmap not always created
Building Standalone Toolchain for NDK 8 on Windows 7 [duplicate]

Categories

HOME
gridview
polymer
gpu
components
permissions
web-hosting
tail
bookmarks
pkcs#11
arangodb
mod-security
why3
angular-dart
renjin
zerobrane
event-sourcing
glyphicons
microsoft-dynamics-nav
null-pointer
wolframalpha
topology
charles
lilypond
user-experience
react-toolbox
epsilon
direct3d11
google-cloud-print
shared
realm-mobile-platform
alm
magento-1.8
custom-controls
memory-address
catch-unit-test
biztalk-rule-engine
activexobject
getpixel
modalviewcontroller
jwplayer6
swagger-php
lint
google-earth-engine
cin
mongoose-schema
ogc
attributeerror
placeholder
importerror
jide
servermanager
builder
trojan
magic-draw
lsyncd
universal-analytics
swingworker
filenet
vcloud-director-rest-api
slackware
pytables
redbeard-ios
centrifuge
adafruit
model-associations
embedding
qiime
lz77
opl
qt5.2
flock
evolus-pencil
lasso
finalbuilder
uncompress
liquid-layout
airbrake
typeconverter
nsurlsessiondatatask
angular-gettext
contrast
metatrader5
cbind
rmysql
unix-socket
parallel-testing
koala
hibernate-entitymanager
breakout
windows-phone-8-sdk
gpars
composite-component
curb
urlrewriter
filemerge
drawtobitmap
massive
cdonts
objective-j
non-clustered-index
feasibility
escrow

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