diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/services/NotificationsService.java b/app/src/main/java/org/communiquons/android/comunic/client/data/services/NotificationsService.java index a0fab4c..8375cc4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/services/NotificationsService.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/services/NotificationsService.java @@ -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.notifications.NotificationsCount; 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; /** @@ -73,7 +74,7 @@ public class NotificationsService extends IntentService { try { //Make a pause - Thread.sleep(2000); + Thread.sleep(30000); } catch (InterruptedException e){ Thread.currentThread().interrupt(); } @@ -81,6 +82,14 @@ public class NotificationsService extends IntentService { //Check if the user is signed in or not if(!new Account(this).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; } @@ -90,60 +99,20 @@ public class NotificationsService extends IntentService { //Check for error if(count == null){ Log.e(TAG, "Could not pull the new number of notifications !"); + removeNotification(); continue; } if(count.getNotificationsCount() > 0 || count.getConversationsCount() > 0){ - Notification.Builder mBuilder; - - //Check which version of the notification system to use - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - - //Create notification channel - CharSequence name = "MainNotificationChannel"; - String description = "Activity notifications"; - int importance = NotificationManager.IMPORTANCE_DEFAULT; - NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, - importance); - mChannel.setDescription(description); - - // Register the channel with the system; you can't change the importance - // or other notification behaviors after this - NotificationManager notificationManager = (NotificationManager) getSystemService( - NOTIFICATION_SERVICE); - notificationManager.createNotificationChannel(mChannel); - - //Create notification builder - mBuilder = new Notification.Builder(this, CHANNEL_ID); - } - else { - //Create notification without channel - mBuilder = new Notification.Builder(this); - mBuilder.setPriority(Notification.PRIORITY_DEFAULT); - } - - //Set notification settings - mBuilder.setSmallIcon(R.drawable.ic_app_rounded); - mBuilder.setContentTitle(getString(R.string.notification_notif_available_title)); - mBuilder.setContentText(getString(R.string.notification_notif_available_content, - count.getNotificationsCount(), count.getConversationsCount())); - - //Create and apply an intent - Intent activityIntent = new Intent(this, MainActivity.class); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0); - mBuilder.setContentIntent(pendingIntent); - - //Get notification manager - ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify( - MAIN_NOTIFICATION_ID, mBuilder.build()); + //Show notification + showNotification(count); } else { //Make sure the notification has been deleted - ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)). - cancel(MAIN_NOTIFICATION_ID); + removeNotification(); } @@ -153,6 +122,64 @@ public class NotificationsService extends IntentService { } + /** + * 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; + + //Check which version of the notification system to use + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + + //Create notification channel + CharSequence name = "MainNotificationChannel"; + String description = "Activity notifications"; + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, + importance); + mChannel.setDescription(description); + + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + NotificationManager notificationManager = (NotificationManager) getSystemService( + NOTIFICATION_SERVICE); + notificationManager.createNotificationChannel(mChannel); + + //Create notification builder + mBuilder = new Notification.Builder(this, CHANNEL_ID); + } + else { + //Create notification without channel + mBuilder = new Notification.Builder(this); + mBuilder.setPriority(Notification.PRIORITY_DEFAULT); + } + + //Set notification settings + mBuilder.setSmallIcon(R.drawable.ic_app_rounded); + mBuilder.setContentTitle(getString(R.string.notification_notif_available_title)); + mBuilder.setContentText(getString(R.string.notification_notif_available_content, + count.getNotificationsCount(), count.getConversationsCount())); + + //Create and apply an intent + Intent activityIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0); + mBuilder.setContentIntent(pendingIntent); + + //Get notification manager to push notification + ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify( + MAIN_NOTIFICATION_ID, mBuilder.build()); + } + + /** + * Remove the notification + */ + private void removeNotification(){ + ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(MAIN_NOTIFICATION_ID); + } + @Override public void onCreate() { super.onCreate(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/PreferencesUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/PreferencesUtils.java new file mode 100644 index 0000000..aa6f01f --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/PreferencesUtils.java @@ -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); + } + +}