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

Android consuming RESTful WCF webservice
Building OpenCV 2.4.5 in android ADT: “ndk-build” not found in PATH
Error with DefaultOAuthProvider in Android
What is good way to pick good matches in Android OpenCV 2.4.4
Scale scrollview contents to fill screen
How to design Activity to use camera for retrieving image properly
How to show a specific location on map in android using Google Map Android API v2?
Getting Content from Imageview in CustomListViewAdapter to share via Facebook
How turning off animation of system operation android programmatically
listView does not refresh with new values
Couldn't connect to MySQL on Android
Cannot reference an android library project in Eclipse
Build Date object with a date and a time
Creating Tile UI Like windows phones [closed]
No repeated contact from content://sms/inbox
Android Emulator: Calendar application will not synchronize with google calendar

Categories

HOME
shell
windows
asp.net-web-api
porting
turing-machines
deep-learning
linkedin
string
gpu
android-studio-2.2
rsa
solver
vscode-extensions
limit
spring-session
joomla3.0
p2p
jquery-ui-sortable
owin
ibeacon-android
mod-security
stellar.js
endpoint
selenium-ide
3nf
standards
intershop
feathersjs
forgot-password
pyramid
identity-management
jmp
sigma.js
nodemcu
guzzle
event-store
google-cloud-print
fat
asymptotic-complexity
firebase-analytics
handler
iis-express
left-join
r-grid
sammy.js
sendbird
slacktextviewcontroller
alphabetical
sessionstorage
tex
signals-slots
attributeerror
dbnull
chronicle-map
android-checkbox
plan-9
servermanager
fwrite
proget
spring-data-couchbase
filehandle
google-photos
pocketsphinx-android
gets
node-request
actor-platform
envi
burrows-wheeler-transform
mongodb-php
hclust
delta
firebase-security
query-by-example
grass
kendo-upload
highland.js
computer-architecture
forwarding
phonertc
vacuum
dronekit-android
omnifaces
showdialog
jbake
structuremap3
post-commit-hook
metatrader5
paw
cancellation
vsx
drupal-nodes
mt4j
light
inputmismatchexception
asdf
poker
node-blade
diem
font-awesome-4.0.0
nokia-s40
httponly
usertype
jqote
event-receiver
http-daemon
globals
maven-1
web-application-project
flvplayback

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App