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

During layout View.getChildAt(i) returns null even when view at that position should exist
How can I add a horizontal line between certain elements in the navigation drawer
Should we set background in LayoutView or ImageView in Android?
Ubuntu 12.0.4 LTS or 14.0.4 LTS for building Android Source files?
Very basic LibGDX game - where is my model, view and controller?
Error instaling Intel Emulator Acelerator HAXM
I would like to make multiples simultaneous outgoing call using twilio
(Android) How can I make the text in my listview item to stay on one row?
change the size of a customized view in program?
Make service lifecycle independent from Activity
Android - XMPP asmack PacketListener throws CalledFromWrongThreadException hierarchy view
Facebook SDK inside library project throws Dalvik error
GraphView not refreshing
Create txt file, write and read from it Android
Calling javascript function from android native code
ListFragment setOnItemClickListener not working

Categories

HOME
httparty
prism
checkstyle
telegram
swt
apache-spark-mllib
solaris
requirements
soa
dojo
3d-reconstruction
hpc
google-admin-sdk
octave
openbugs
zerobrane
screen-readers
null-pointer
fileserver
soci
production
google-maps-autocomplete
user-experience
guzzle
hta
exploit
gmock
beautifier
fable-f#
mat
tizen-tv
alm
autodesk-designautomation
deltaspike
erb
sim-toolkit
alphabetical
css-position
metawidget
resolution
postgresql-8.4
movilizer
placeholder
apache-spark-dataset
stylecop
libgphoto2
proget
singleinstance
backbone-collections
py2neo
sandcastle
donations
mapxtreme
reporting-services-2012
ng-grid
kango-framework
libav
thread-sleep
user-management
concurrentmodification
decimalformat
fuelcms
amiga
mraid
cakephp-2.7
qtwebengine
cosine-similarity
webmatrix-3
android-vibration
ghcjs
contrast
metatrader5
xml-namespaces
create.js
jqmodal
mobility
jjaql
wxformbuilder
fpdi
menubar
multitasking
windows-embedded-standard
jammer
cocoalibspotify-2.0
cnf
asdf
facebook-timeline
composite-component
mysql-error-2013
robotlegs
tree-balancing
iboutlet
oracle-enterprise-linux
isa-swizzling
stress
agility.js
listactivity
drawtobitmap
multiplatform
cac
purepdf

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