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

Make SQLDatabase limited
Attach a progress bar to an android activity
Facing problems watching the videos in videoview upon a list item click in the listview
Android: ListView & customAdapter - List when clicked showes info of 2 items / editText not clickable
Expanding the SearchActionItem without adding to backstack
App not for tablets [duplicate]
Call method onClickListener
How to move a layout content outside the screen android
Android Bluetooth Socket
TabPageIndicator not style properly inside Fragment
Is there a way to implement rounded corners to a Mapfragment?
Json parsing type mismatch error in android
Getting black screenshot by using android screenshot library
Android: keyboard hidden button
phonegap/cordova android crash when getting facebook-synced image from photo library
How scroll a fragment horizontally in Android

Categories

HOME
oracle12c
webdriver
interface
apache-spark-mllib
replace
opendj
google-cloud-functions
permissions
web-hosting
vsm
google-cloud-datalab
performancepoint
arangodb
vimdiff
endeca
android-securityexception
symbol
package-managers
red5
forgot-password
imagemap
inno-download-plugin
monit
ckfinder
froala
tiki-wiki
interbase
amazon-quicksight
tpm
testcomplete
magento-1.8
corenlp-server
jbutton
angularjs-material
fstream
spring-security4
simplecv
jett
sammy.js
keras-layer
fiware-cygnus
parse-tree
google-knowledge-graph
jvmti
tasm
azure-arm
http-request
berkeley-db-je
monkey-testing
optimizely
fwrite
jboss-4.2.x
magento-2.0.7
git-flow
excel-dna
oozie-coordinator
cpu-cache
google-photos
baidu
materialized-path-pattern
microstation
centrifuge
vulcanize
neon
mta
nikeplus-api
hclust
installshield-2010
eigenvector
swizzling
nservicebus5
lua-telegram-bot
bizagi
np-spring
directdraw
iokit
wiredep
include-guards
cgi-bin
haskell-warp
ansi
cakephp-2.7
dot.js
bcache
delayed-execution
mruby
entry-point
raygun
with-statement
build-script
matrix-inverse
tinyxml
paste
maven-scm
drupal-nodes
android-audiorecord
android-usb
code-duplication
unit-of-work
cmmi
kaleidoscope
time.h
stacky
cascadingdropdown
panda3d
correlated-subquery
stress
pinch
screen-size
timthumb
timertask
maven-1
lxr
scrubyt
idatareader

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