mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Improved notification system.
This commit is contained in:
		@@ -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,11 +99,36 @@ 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){
 | 
			
		||||
 | 
			
		||||
                //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;
 | 
			
		||||
 | 
			
		||||
        //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);
 | 
			
		||||
        mBuilder.setContentIntent(pendingIntent);
 | 
			
		||||
 | 
			
		||||
                //Get notification manager
 | 
			
		||||
        //Get notification manager to push notification
 | 
			
		||||
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(
 | 
			
		||||
                MAIN_NOTIFICATION_ID, mBuilder.build());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
            else {
 | 
			
		||||
 | 
			
		||||
                //Make sure the notification has been deleted
 | 
			
		||||
                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).
 | 
			
		||||
                        cancel(MAIN_NOTIFICATION_ID);
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Log.v(TAG, "Stop service");
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Remove the notification
 | 
			
		||||
     */
 | 
			
		||||
    private void removeNotification(){
 | 
			
		||||
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(MAIN_NOTIFICATION_ID);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user