mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Make notification service lives.
This commit is contained in:
		@@ -0,0 +1,35 @@
 | 
			
		||||
package org.communiquons.android.comunic.client.data.notifications;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Notifications count service
 | 
			
		||||
 *
 | 
			
		||||
 * @author Pierre HUBERT
 | 
			
		||||
 * Created by pierre on 4/9/18.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class NotificationsCount {
 | 
			
		||||
 | 
			
		||||
    //Private fields
 | 
			
		||||
    private int notificationsCount;
 | 
			
		||||
    private int conversationsCount;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //Set and get notifications count
 | 
			
		||||
    public void setNotificationsCount(int notificationsCount) {
 | 
			
		||||
        this.notificationsCount = notificationsCount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getNotificationsCount() {
 | 
			
		||||
        return notificationsCount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //Set and get conversations count
 | 
			
		||||
    public void setConversationsCount(int conversationsCount) {
 | 
			
		||||
        this.conversationsCount = conversationsCount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getConversationsCount() {
 | 
			
		||||
        return conversationsCount;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,67 @@
 | 
			
		||||
package org.communiquons.android.comunic.client.data.notifications;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import org.communiquons.android.comunic.client.api.APIRequest;
 | 
			
		||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
 | 
			
		||||
import org.communiquons.android.comunic.client.api.APIResponse;
 | 
			
		||||
import org.json.JSONObject;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Notifications helper
 | 
			
		||||
 *
 | 
			
		||||
 * @author Pierre HUBERT
 | 
			
		||||
 * Created by pierre on 4/9/18.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
public class NotificationsHelper {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Application context
 | 
			
		||||
     */
 | 
			
		||||
    private Context mContext;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Notifications helper constructor
 | 
			
		||||
     *
 | 
			
		||||
     * @param context The context of the application
 | 
			
		||||
     */
 | 
			
		||||
    public NotificationsHelper(Context context){
 | 
			
		||||
 | 
			
		||||
        //Save context
 | 
			
		||||
        this.mContext = context.getApplicationContext();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the notifications count
 | 
			
		||||
     *
 | 
			
		||||
     * @return Notifications count / NULL in case of failure
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public NotificationsCount pullCount(){
 | 
			
		||||
 | 
			
		||||
        //Perform an API request
 | 
			
		||||
        APIRequestParameters params = new APIRequestParameters(mContext,
 | 
			
		||||
                "notifications/count_all_news");
 | 
			
		||||
 | 
			
		||||
        //Try to perform the request and parse results
 | 
			
		||||
        try {
 | 
			
		||||
 | 
			
		||||
            APIResponse response = new APIRequest().exec(params);
 | 
			
		||||
 | 
			
		||||
            //Try to parse results
 | 
			
		||||
            JSONObject object = response.getJSONObject();
 | 
			
		||||
            NotificationsCount res = new NotificationsCount();
 | 
			
		||||
            res.setNotificationsCount(object.getInt("notifications"));
 | 
			
		||||
            res.setConversationsCount(object.getInt("conversations"));
 | 
			
		||||
            return res;
 | 
			
		||||
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +1,20 @@
 | 
			
		||||
package org.communiquons.android.comunic.client.data.services;
 | 
			
		||||
 | 
			
		||||
import android.app.IntentService;
 | 
			
		||||
import android.app.Notification;
 | 
			
		||||
import android.app.NotificationChannel;
 | 
			
		||||
import android.app.NotificationManager;
 | 
			
		||||
import android.app.PendingIntent;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
import android.util.Log;
 | 
			
		||||
 | 
			
		||||
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.ui.activities.MainActivity;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Notifications service
 | 
			
		||||
 *
 | 
			
		||||
@@ -24,6 +34,21 @@ public class NotificationsService extends IntentService {
 | 
			
		||||
     */
 | 
			
		||||
    private boolean run;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Notifications helper
 | 
			
		||||
     */
 | 
			
		||||
    private NotificationsHelper mNotifHelper;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Notification channel ID
 | 
			
		||||
     */
 | 
			
		||||
    private final String CHANNEL_ID = "MainNotifChannel";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Main notification ID
 | 
			
		||||
     */
 | 
			
		||||
    private final static int MAIN_NOTIFICATION_ID = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Public constructor
 | 
			
		||||
     */
 | 
			
		||||
@@ -39,6 +64,9 @@ public class NotificationsService extends IntentService {
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onHandleIntent(@Nullable Intent intent) {
 | 
			
		||||
 | 
			
		||||
        //Create notifications helper
 | 
			
		||||
        mNotifHelper = new NotificationsHelper(getApplicationContext());
 | 
			
		||||
 | 
			
		||||
        Log.v(TAG, "Start service");
 | 
			
		||||
 | 
			
		||||
        while(run){
 | 
			
		||||
@@ -50,7 +78,74 @@ public class NotificationsService extends IntentService {
 | 
			
		||||
                Thread.currentThread().interrupt();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Log.v(TAG, "Hey there, service !");
 | 
			
		||||
            //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.");
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //Pull the number of notifications
 | 
			
		||||
            NotificationsCount count = mNotifHelper.pullCount();
 | 
			
		||||
 | 
			
		||||
            //Check for error
 | 
			
		||||
            if(count == null){
 | 
			
		||||
                Log.e(TAG, "Could not pull the new number of notifications !");
 | 
			
		||||
                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());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            else {
 | 
			
		||||
 | 
			
		||||
                //Make sure the notification has been deleted
 | 
			
		||||
                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).
 | 
			
		||||
                        cancel(MAIN_NOTIFICATION_ID);
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -147,4 +147,6 @@
 | 
			
		||||
    <string name="fragment_settings_title">Settings</string>
 | 
			
		||||
    <string name="preference_background_notif_title">Background notifications refresh</string>
 | 
			
		||||
    <string name="preference_background_notif_summary">This allow you to stay informed about the latest notifications on Comunic by checking at a regular interval the existence of notifications, even if the application is closed.</string>
 | 
			
		||||
    <string name="notification_notif_available_title">New activity on Comunic</string>
 | 
			
		||||
    <string name="notification_notif_available_content"> %1$d notification(s) and %2$d unread conversations(s)</string>
 | 
			
		||||
</resources>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user