diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsCount.java b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsCount.java new file mode 100644 index 0000000..8a45bec --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsCount.java @@ -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; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java new file mode 100644 index 0000000..4bb3541 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/notifications/NotificationsHelper.java @@ -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; + } + } + +} 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 5f25119..a0fab4c 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 @@ -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); + + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1c705df..d79066f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -147,4 +147,6 @@ Settings Background notifications refresh 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. + New activity on Comunic + %1$d notification(s) and %2$d unread conversations(s)