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

Is removeUpdates() optional when changing GPS update time?
Android APK Auto Update
How to setText from intent data
cannot release bluetooth headset on Android
how use load image by twowayview?
Is it possible to read the minSDKVersion from AndroidManifest at runtime?
Error on sqlite database with 2 table creation in android, unfortunately,“dbtest2 has stopped”
After using rngd, [hwrng] continues to max out entropy
Add ImageView after text
why `ifroom` does not work - android studio
Unable to run Android - DeviceReady in cordova
ViewPager AutoScroll not Working Properly
How to Scan iBeacon in Android Wear (Ambient Mode)
How to send Captured Image to second activity?
App is not calculating distance and speed android
Swiping on a item in the listview should load another activity associated with that particular item swipped

Categories

HOME
shell
turing-machines
linkedin
3d
composite-primary-key
soa
nested-if
spring-session
iis-6
vert.x
pcl-crypto
siddhi
cumulocity
esoteric-languages
standards
firefox-addon
google-content-api
sigma.js
comparator
fable-f#
jackson-modules
delimiter
outliers
comparable
aws-codecommit
xbox
record
cognos-bi
cart
carriage-return
browser-extension
yowsup
xajax
sql-server-ce
dm-script
connection-pool
comparison-operators
apriori
wmp
linqpad
dbnull
launchctl
x-ray
yii2-user
otp
libjpeg
mathcad
android-instrumentation
pocketsphinx-android
cronexpression
mta
rspec2
jquery-jscrollpane
omniauth-facebook
beanstalk
invoke-sqlcmd
piracy-prevention
sapi
linklabel
gocql
video-player
opserver
dock
typeconverter
cakephp-2.7
document-repository
deform
webfinger
rickshaw
tweenlite
named-parameters
mintty
live555
fpdi
gae-quotas
koala
multiautocompletetextview
light
mongohq
process-explorer
facebook-timeline
http-status-code-410
faye
drawtobitmap
dataform
aspmenu
murmurhash
lobo-cobra
tui
windows-identity

Resources

Encrypt Message