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

Create FloatingActionButton programmatically (without xml)
Change Background of RecyclerView Item at specific position
Local notification with alarm not work in day
MP4 Video Not Playing in Chrome 56 or 57 on Android
How to disable alert dialog close, if editText inside Alert dialog is empty
No such file or directory when trying to read from path
how to implement reverse CircularReveal animation to close BottomSheetDialog
One PagerAdapter for multiple fragments
Firebase Storage Android | Offline capabilities for music
how to fix gradle daemon running for very long time?
cx_Oracle not working on Android
ANDROID-MQTT :Unable to connect to server (32103)
Android- Upload image to Firebase under a specific user
How to wait for elements to show up
ContextMenu Listener Recycler view
Calabash android.util.AndroidException: INSTRUMENTATION_FAILED

Categories

HOME
fabricjs
cheerio
linkedin
c99
telegram
fpga
angular-formly
case
mysql-workbench
mstest
grafana
access
google-cloud-functions
database-normalization
maxscript
nfs
special-characters
32bit-64bit
ibeacon-android
fhir
gkturnbasedmatch
shared-libraries
do-while
package-managers
firefox-addon
cfml
nodemcu
articulate-storyline
visual-studio-extensions
perlbrew
xcode-ui-testing
opentk
database-comparison
xcode7.3
loading
health-monitoring
css-position
cin
c9.io
levenshtein-distance
skylink
plan-9
magento-2.0.7
sonarlint-eclipse
manova
context-switch
colorbar
gpg-signature
mathcad
django-1.10
bootstrap-tags-input
smartfoxserver
chromium-os
meteor-packages
pacemaker
watch-os-2
respondcms
pre-build-event
shopping
gcloud-ruby
findfirst
iokit
extjs-grid
uncompress
ms-access-2000
android-broadcast
broadband
dbaccess
jbake
browserify-shim
gii
ignite-ui
matrix-inverse
mutual-authentication
radiant
file-not-found
menubar
qscrollarea
multitasking
neos-server
joomla-template
preload
joomla3.1
nokia-s40
extconf.rb
ilasm
denied
expression-blend-4
android-holo-everywhere
listactivity
easyb
qtabbar
cfwindow
unmanagedresources
online-storage
netbeans6.1

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile