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

Cannot be resolved or is not a field facebook
image not displaying inside an activity after capturing the image from camera in android
Android ADT build path issue, how to fix?
Use UncaughtExceptionHandler to do what Android usually does plus extra functionality I need
How to properly implement an Adapter for a ListView
Fatal Exception IllegalStateException. Could not execute method of the activity
Google navigation dialog box “you're navigating a route ”
Android activity not found in android manifest
Parse - uploading data sometimes needs forever and does never finish
Long.toString(long n) yields non-static access error
How do I hook into the back-pressed event when using a DialogFragment?
Chat algorithm with websockets and gcm
Is it possible to detect Android application uninstall?
GCM Re-registration each time the App starts
Samsung Duos S2 is not connecting with Eclipse
Repo Sync : Fatal Error

Categories

HOME
algorithm
paraview
merge
amp-html
nunit
pda
pyqt
serialization
hessian
knitr
owl-carousel
simulink
google-apps
mips
jquery-ui
spring-session
computer-science
extjs4.2
categorical-data
reactive-programming
startup
spring-social
dynatrace
enterprise-architect
cumulocity
package-managers
glyphicons
google-data-studio
video-editing
windows-xp
ag
dataflow-diagram
direct3d11
mount
tiki-wiki
siri
shared
delimiter
amazon-quicksight
rpgle
browser-sync
winpe
google-earth
catch-unit-test
jett
openwhisk
yowsup
processwire
searchkit
xdocreport
filesystemwatcher
verifiable-c
numberpicker
ssrs-2014
uiimage
textsum
lampp
spring-integration-sftp
mschart
pair-programming
git-flow
quicktime
pybrain
ampps
filenet
renaming
sandcastle
mapxtreme
redistributable
burrows-wheeler-transform
delta
kango-framework
boost-serialization
phpwebsocket
spring-retry
concurrentmodification
avisynth
xvim
binomial-theorem
cgi-bin
doctype
keyup
dbaccess
addressing-mode
sonarqube-5.0
iso-prolog
paw
wcm
create.js
angular-carousel
mutual-authentication
android-framework
accesscontrolexception
visual-studio-express
http-patch
scrypt
mongohq
epiceditor
actiondispatch
virtual-pc
bjyauthorize
argouml
faye
role
event-receiver
dot-emacs
alsb
xpsdocument
unmanagedresources
dsoframer

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