Improved notification system.

This commit is contained in:
Pierre 2018-04-09 18:16:13 +02:00
parent e0b1066ded
commit 871b12a07b
2 changed files with 101 additions and 45 deletions

View File

@ -13,6 +13,7 @@ import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.Account;
import org.communiquons.android.comunic.client.data.notifications.NotificationsCount; import org.communiquons.android.comunic.client.data.notifications.NotificationsCount;
import org.communiquons.android.comunic.client.data.notifications.NotificationsHelper; import org.communiquons.android.comunic.client.data.notifications.NotificationsHelper;
import org.communiquons.android.comunic.client.data.utils.PreferencesUtils;
import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.activities.MainActivity;
/** /**
@ -73,7 +74,7 @@ public class NotificationsService extends IntentService {
try { try {
//Make a pause //Make a pause
Thread.sleep(2000); Thread.sleep(30000);
} catch (InterruptedException e){ } catch (InterruptedException e){
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
@ -81,6 +82,14 @@ public class NotificationsService extends IntentService {
//Check if the user is signed in or not //Check if the user is signed in or not
if(!new Account(this).signed_in()){ if(!new Account(this).signed_in()){
Log.v(TAG, "Skip notifications refresh because the user is not signed in."); Log.v(TAG, "Skip notifications refresh because the user is not signed in.");
removeNotification();
continue;
}
//Check if notification pull is disabled
if(!PreferencesUtils.getBoolean(this, "enable_background_notification_refresh", true)) {
Log.v(TAG, "Skip notifications refresh because the user disabled the option.");
removeNotification();
continue; continue;
} }
@ -90,11 +99,36 @@ public class NotificationsService extends IntentService {
//Check for error //Check for error
if(count == null){ if(count == null){
Log.e(TAG, "Could not pull the new number of notifications !"); Log.e(TAG, "Could not pull the new number of notifications !");
removeNotification();
continue; continue;
} }
if(count.getNotificationsCount() > 0 || count.getConversationsCount() > 0){ if(count.getNotificationsCount() > 0 || count.getConversationsCount() > 0){
//Show notification
showNotification(count);
}
else {
//Make sure the notification has been deleted
removeNotification();
}
}
Log.v(TAG, "Stop service");
}
/**
* Create and display the notification accordingly to the given count information
*
* @param count The number of new notifications
*/
private void showNotification(NotificationsCount count){
Notification.Builder mBuilder; Notification.Builder mBuilder;
//Check which version of the notification system to use //Check which version of the notification system to use
@ -134,23 +168,16 @@ public class NotificationsService extends IntentService {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
mBuilder.setContentIntent(pendingIntent); mBuilder.setContentIntent(pendingIntent);
//Get notification manager //Get notification manager to push notification
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify( ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(
MAIN_NOTIFICATION_ID, mBuilder.build()); MAIN_NOTIFICATION_ID, mBuilder.build());
} }
else { /**
* Remove the notification
//Make sure the notification has been deleted */
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)). private void removeNotification(){
cancel(MAIN_NOTIFICATION_ID); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(MAIN_NOTIFICATION_ID);
}
}
Log.v(TAG, "Stop service");
} }
@Override @Override

View File

@ -0,0 +1,29 @@
package org.communiquons.android.comunic.client.data.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
/**
* Preference utilities
*
* @author Pierre HUBERT
* Created by pierre on 4/9/18.
*/
public class PreferencesUtils {
/**
* Get a boolean preference
*
* @param context The context of the application
* @param key The name of the key to get
* @param def The default value in case the value was not found
* @return The preference value (if found) or the default value
*/
public static boolean getBoolean(Context context, String key, boolean def){
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
return sharedPrefs.getBoolean(key, def);
}
}