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

Speeding up scrypt on android
Blur text in TextView unexpected behaviour
merge two projects in one using library
Text Selection not working in custom listview Android
how to remain login in fb sdk after rebooting the android app
How to check if the Apps current activity belongs to a package?
Firebase is not saving Facebook user in Auth list
Modifying contents of Android .aar file / Converting to and from .zip format
LibGdx not using textures (Mixing OpenGL native and LibGDX)
Sequential exeution of operations that modify UI
Firebase 2.0 Realtime Database data structure - Android
Unity android app's launcher icon suddenly changes to android default logo, and the name to package name
Converting dp into pixels for multiple screen support
How to proxy desktop browser traffic through Android/IOS App?
OutOfMemoryError that I don't know when or why happened
Android Background Task Freezes UI Thread - Updating Firebase

Categories

HOME
gridview
adal
merge
telegram
apache-jena
rdbms
object
toolbox
seo
3d-reconstruction
mapbox
odoo-8
performancepoint
commonjs
appcelerator-studio
github-enterprise
android-securityexception
emoji
line
datastage
angular-dart
tput
dynatrace
internationalization
glyphicons
intershop
dreamweaver
lilypond
poedit
imageresizer
gmock
game-theory
tiki-wiki
outliers
python-2.6
openwhisk
fossil
gdata
diacritics
chronicle-map
importerror
getchar
ovf
serde
globalize
er-diagram
trigger.io
cd-burning
sandcastle
cvxpy
smartfoxserver
cudd
myspace
vulcanize
model-associations
delta
kango-framework
phpgrid
vigenere
video-player
apache-commons-daemon
bufferedinputstream
zend-currency
java-security
psr-4
grails-domain-class
reflector
gnumeric
dot42
terminate
multiautocompletetextview
drupal-nodes
hibernate-entitymanager
http-patch
scringo
fragmenttransaction
veracity
faye
boost-signals
objectquery
convention
winmain
mysql-error-1205
django-paypal
sifr
pagemethods
glui
entitykey

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