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

Injecting key events from android service
Android: Find location from location name. Is Android's geocoder the correct way?
Custom Listview : span listitems on selection
Android fullscreen activity without battery icon
Ionic refresher not working properly
Empty savedInstanceState Bundle when restoring a Fragment after double Rotation in replacing Fragment
What is the difference between Fragment.isVisible() and isVisibleToUser
AnimationDrawable not playing. Not called from onCreate
why adb shell doesn't support wildcard?
udp sender under android via USB > ethernet
Layout_margin is not working
How can I incorporate Gradle into my existing project on Eclipse?
In game Images disappear on Android device if i run from widget, but not when I install apk first time
Android OnSystemUiVisibilityChangeListener fires in Portrait only
Two nested async tasks in android
How can I get device Locale or Language with PhoneGap Build?

Categories

HOME
google-app-engine
merge
composite-primary-key
vscode-extensions
xbee
tvos
g++
mxgraph
filechannel
eip
bitnami
do-while
glyphicons
template-engine
asp.net-mvc-viewmodel
articulate-storyline
pimcore
pchart
jquery-callback
siri
slidetoggle
jackson-modules
android-maps
rpgle
testcomplete
send
mustache
laravel-eloquent
simplecv
papaparse
openerp-7
docfx
wurfl
demo
spring-mvc-test
knime
strftime
procfile
flask-restful
robomongo
geotiff
payara-micro
video-conferencing
android-testing
er-diagram
universal-analytics
avspeechsynthesizer
upx
nullable
findfirst
aspen
realsense
jparsec
user-management
directdraw
linklabel
toolkit
doctype
zscript
baasbox
ocra
jsvc
nsurlsessiondatatask
actionfilterattribute
mmwormhole
qtwebengine
ssis-data-flow
iso-prolog
create.js
git-repo
sphinxql
jammer
ice-cube
cmmi
nevron
time.h
actiondispatch
pyunit
urlrewriter
tournament
drawtobitmap
msr
tmx
unmanagedresources
ifilter
wcf-proxy
pnrp
netbeans6.1

Resources

Encrypt Message