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 CameraSource works differently for multiple devices and doesn't rotate image
Error running emulator on android
Can't run start.sh for GCM Playground sample application
Button click event closes Activity insted of running ASyncTask
How do I make EditText invisible after input?
Unable to get IMEI in dubbed android phone?
Getting child view reaches to top in ScrollView
how to Hide the Status Bar on Android 4.1 and Higher
Google play services crashing on android mobile yu yureka with cynogen mod12
<button> tag is not taking in Html.fromHtml in android
How can I ensure that my Android 6 Cordova app prompts the user to give permission before attempting to access the filesystem?
Android: recycler view over tab bar
Bottom Sheet Behavior needed to be shown only on button click?
GCM click_action property on payload is not appearing
react-native run-android red screen of death
Create a RecyclerView with both horizontal and vertical scrolling

Categories

HOME
notepad++
google-app-engine
time-complexity
codenvy
webdriver
interface
sitecore
sublimetext
r-googlesheets
bitbucket-api
google-apps
cakephp-3.4
3d-reconstruction
babeljs
special-characters
pega
jsonpath
supervisor
nancy
radgridview
onedrive-api
firefox-addon
wolframalpha
mapstruct
guzzle
string-concatenation
easeljs
android-things
bioinformatics
testcomplete
gitlab-api
cognos-bi
wurfl
apns-php
urlsession
datacachefactory
parser-generator
print-css
outputcache
file-sharing
chai-as-promised
spring-integration-sftp
geotiff
appdomain
ngmaterial
libjpeg
jodd
expectations
redis-cluster
vhosts
decimalformat
fuelcms
sapscript
mraid
zxspectrum
actionfilterattribute
java-security
xml-dsig
dct
kogrid
mvc-editor-templates
nebula
jquery-1.9
pagedown
superscrollorama
alice
dopostback
correlated-subquery
pinch
expression-blend-4
landscape-portrait
drawtobitmap
resharper-6.0
office-2007
interprocess
software-lifecycle
linfu-dynamicproxy

Resources

Encrypt Message