mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-12-27 22:18:58 +00:00
Display the number of unread conversation and notifications on AppBar
This commit is contained in:
parent
13f109b5fa
commit
0cd0b3f7d5
@ -7,6 +7,7 @@ import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
@ -16,6 +17,8 @@ import org.communiquons.android.comunic.client.data.helpers.NotificationsHelper;
|
||||
import org.communiquons.android.comunic.client.data.utils.PreferencesUtils;
|
||||
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Notifications service
|
||||
*
|
||||
@ -30,6 +33,18 @@ public class NotificationsService extends IntentService {
|
||||
*/
|
||||
private static final String TAG = "NotificationsService";
|
||||
|
||||
/**
|
||||
* Broadcast action
|
||||
*/
|
||||
public static final String BROADCAST_ACTION =
|
||||
"org.communiquons.android.comunic.NotificationService.BROADCAST";
|
||||
|
||||
/**
|
||||
* Notification extras
|
||||
*/
|
||||
public static final String BROADCAST_EXTRA_NUMBER_NOTIFICATIONS = "NumberNotifications";
|
||||
public static final String BROADCAST_EXTRACT_UNREAD_CONVERSATIONS = "UnreadConversations";
|
||||
|
||||
/**
|
||||
* Notification channel ID
|
||||
*/
|
||||
@ -54,7 +69,7 @@ public class NotificationsService extends IntentService {
|
||||
/**
|
||||
* Last notification count
|
||||
*/
|
||||
private NotificationsCount mLastCount;
|
||||
private static NotificationsCount mLastCount;
|
||||
|
||||
|
||||
/**
|
||||
@ -136,6 +151,12 @@ public class NotificationsService extends IntentService {
|
||||
//Save last notifications count
|
||||
mLastCount = count;
|
||||
|
||||
//Create an intent and push nut data
|
||||
Intent pushIntent = new Intent(BROADCAST_ACTION)
|
||||
.putExtra(BROADCAST_EXTRA_NUMBER_NOTIFICATIONS, count.getNotificationsCount())
|
||||
.putExtra(BROADCAST_EXTRACT_UNREAD_CONVERSATIONS, count.getConversationsCount());
|
||||
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(pushIntent);
|
||||
}
|
||||
|
||||
Log.v(TAG, "Stop service");
|
||||
@ -166,6 +187,7 @@ public class NotificationsService extends IntentService {
|
||||
// or other notification behaviors after this
|
||||
NotificationManager notificationManager = (NotificationManager) getSystemService(
|
||||
NOTIFICATION_SERVICE);
|
||||
assert notificationManager != null;
|
||||
notificationManager.createNotificationChannel(mChannel);
|
||||
|
||||
//Create notification builder
|
||||
@ -189,7 +211,7 @@ public class NotificationsService extends IntentService {
|
||||
mBuilder.setContentIntent(pendingIntent);
|
||||
|
||||
//Get notification manager to push notification
|
||||
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(
|
||||
((NotificationManager) Objects.requireNonNull(getSystemService(NOTIFICATION_SERVICE))).notify(
|
||||
MAIN_NOTIFICATION_ID, mBuilder.build());
|
||||
}
|
||||
|
||||
@ -197,7 +219,8 @@ public class NotificationsService extends IntentService {
|
||||
* Remove the notification
|
||||
*/
|
||||
private void removeNotification(){
|
||||
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(MAIN_NOTIFICATION_ID);
|
||||
((NotificationManager) Objects.requireNonNull(getSystemService(NOTIFICATION_SERVICE)))
|
||||
.cancel(MAIN_NOTIFICATION_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,13 +2,17 @@ package org.communiquons.android.comunic.client.ui.activities;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
@ -25,6 +29,7 @@ import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
|
||||
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
||||
import org.communiquons.android.comunic.client.data.helpers.DebugHelper;
|
||||
import org.communiquons.android.comunic.client.data.models.NotificationsCount;
|
||||
import org.communiquons.android.comunic.client.data.runnables.FriendRefreshLoopRunnable;
|
||||
import org.communiquons.android.comunic.client.data.services.NotificationsService;
|
||||
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
||||
@ -90,6 +95,29 @@ public class MainActivity extends AppCompatActivity implements
|
||||
*/
|
||||
private NavigationBar mNavBar;
|
||||
|
||||
/**
|
||||
* Broadcast receiver
|
||||
*/
|
||||
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
if(intent.getAction() == null)
|
||||
return;
|
||||
|
||||
//Notifications number update
|
||||
if(intent.getAction().equals(NotificationsService.BROADCAST_ACTION)){
|
||||
NotificationsCount count = new NotificationsCount();
|
||||
assert intent.getExtras() != null;
|
||||
count.setNotificationsCount(intent.getExtras().getInt(
|
||||
NotificationsService.BROADCAST_EXTRA_NUMBER_NOTIFICATIONS));
|
||||
count.setConversationsCount(intent.getExtras().getInt(
|
||||
NotificationsService.BROADCAST_EXTRACT_UNREAD_CONVERSATIONS));
|
||||
updateNumberNotifications(count);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -136,6 +164,10 @@ public class MainActivity extends AppCompatActivity implements
|
||||
if (savedInstanceState == null){
|
||||
openNotificationsFragment(false);
|
||||
}
|
||||
|
||||
//Receive broadcasts
|
||||
IntentFilter intentFilter = new IntentFilter(NotificationsService.BROADCAST_ACTION);
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -302,7 +334,18 @@ public class MainActivity extends AppCompatActivity implements
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the number of unread notifications
|
||||
*
|
||||
* @param count New number of notifications
|
||||
*/
|
||||
public void updateNumberNotifications(NotificationsCount count){
|
||||
mNavBar.getItemIdentifierView(R.id.action_notifications).setNumberNews(
|
||||
count.getNotificationsCount());
|
||||
|
||||
mNavBar.getItemIdentifierView(R.id.action_conversations).setNumberNews(
|
||||
count.getConversationsCount());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -126,7 +126,7 @@ public class NavigationBar extends BaseFrameLayoutView implements NavigationBarI
|
||||
* @param index The index of the item to get
|
||||
* @return Related view
|
||||
*/
|
||||
public View getItemIndexView(int index){
|
||||
public NavigationBarItem getItemIndexView(int index){
|
||||
return mItems.get(index);
|
||||
}
|
||||
|
||||
@ -136,7 +136,7 @@ public class NavigationBar extends BaseFrameLayoutView implements NavigationBarI
|
||||
* @param id The identifier of the item to get
|
||||
* @return Related view
|
||||
*/
|
||||
public View getItemIdentifierView(int id){
|
||||
public NavigationBarItem getItemIdentifierView(int id){
|
||||
return getItemIndexView(MenuUtils.MenuIdentifierToIndex(mMenu, id));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user