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,60 +99,20 @@ 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){
Notification.Builder mBuilder; //Show notification
showNotification(count);
//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 { else {
//Make sure the notification has been deleted //Make sure the notification has been deleted
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)). removeNotification();
cancel(MAIN_NOTIFICATION_ID);
} }
@ -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 @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();

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);
}
}