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 do I enter a name parameter while signing up a user in Firebase for Android
Login to twitter always goes to callback failure method
Why onDraw() is not called if Custom View extends ViewGroup?
Can't see the newest taken picture in the gallery?
#120 - Invalid album ID while uploading an image to Facebook group
strange broken simbol rendered in mobile chrome
Android consumable in app purchase flow called twice launchPurchaseFlow()
Mobile network 3g/4g randomly disconnects from the server
Binding several views to a Listener with multiple methods in Butterknife
Different display interface on the same devices
Encountered unregistered class ID: 12 SnappyDB
React native listview does not show all the rows
No view found error for a fragment with a ListView with a custom Adapter
android app crash since fab change
Android Studio Search View DatabaseTable can't be applied
GCM receiver in separate module

Categories

HOME
beautifulsoup
xml
scikit-learn
github-for-windows
gpu
serialization
obfuscation
mstest
access
google-cloud-functions
nsbundle
hugo
iis-6
fosrestbundle
32bit-64bit
siddhi
filechannel
alpacajs
dynamic-sql
easyphp
windows-xp
microsoft-ocr
articulate-storyline
jquery-callback
outliers
8051
gitlab-api
python-2.6
dst
kudu
git-squash
clickjacking
gnat-gps
zoomify
filesystemwatcher
recurrent-neural-network
typesafe-config
skylink
apriori
attributeerror
microsoft-metro
print-css
wc
httrack
geotiff
custom-font
stdmap
pdf-conversion
bbpress
cpu-cache
centrifuge
lrs
mapinfo
hclust
vorpal.js
cda
nupic
beanstalk
state-restoration
binomial-theorem
zend-currency
bcache
exceldatareader
showdialog
mruby
wcm
nsmenu
raygun
xml-dsig
dct
buffering
extjs2
android-audiorecord
jammer
tlf
pstack
sentestingkit
boost-signals
inkcanvas
silverlight-oob
feasibility
mathematical-notation
todos
soappy
pbcopy

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile