/** * Create and show a simple notification containing the channels name where the new announcement * was posted. */ private void sendAnnouncementNotification(int channelId) { ChannelDatabaseManager channelDBM = new ChannelDatabaseManager(this); Channel channel = channelDBM.getChannel(channelId); int numberOfAnnouncements = channelDBM.getNumberOfUnreadAnnouncements(channelId); Intent intent; boolean loggedIn = Util.getInstance(this).getLoggedInModerator() != null; if (loggedIn) { // User is logged in as local moderator. intent = new Intent(this, ModeratorChannelActivity.class); } else { // User isn't logged in. intent = new Intent(this, ChannelActivity.class); } intent.putExtra("channelId", channelId); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Create a PendingIntent containing the entire back stack. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); if (loggedIn) { stackBuilder.addParentStack(ModeratorChannelActivity.class); } else { stackBuilder.addParentStack(ChannelActivity.class); } stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(channelId, PendingIntent.FLAG_UPDATE_CURRENT); // Set channel icon as large icon. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), ChannelController.getChannelIcon(channel)); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_notification_icon_1) .setColor(ContextCompat.getColor(this, R.color.uni_main_primary)) .setLargeIcon(bitmap) .setContentTitle(channel.getName()) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); if (numberOfAnnouncements > 1) { notificationBuilder.setContentText(getString(R.string.push_message_new_announcements)); notificationBuilder.setNumber(numberOfAnnouncements); } else { notificationBuilder.setContentText(getString(R.string.push_message_new_announcement)); } NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notificationBuilder.build()); }
/** * This method will be called when a list of announcements is posted to the EventBus. * * @param event The bus event containing a list of announcement objects. */ public void onEvent(BusEventAnnouncements event) { // Unregister this instance. For new push messages the new instance will be registered in // onCreate(). EventBus.getDefault().unregister(this); Log.d(TAG, event.toString()); ChannelController.storeAnnouncements(getApplicationContext(), event.getAnnouncements()); // Check if new announcements were loaded. if (!event.getAnnouncements().isEmpty()) { int channelId = event.getAnnouncements().get(0).getChannelId(); // Defines whether the user should be notified or not. boolean showNotification = false; NotificationSettings notificationSettings = settingsDBM.getChannelNotificationSettings(channelId); if (notificationSettings == NotificationSettings.GENERAL) { notificationSettings = settingsDBM.getSettings().getNotificationSettings(); } switch (notificationSettings) { case ALL: showNotification = true; break; case PRIORITY: for (Announcement announcement : event.getAnnouncements()) { if (announcement.getPriority() == Priority.HIGH) { showNotification = true; break; } } break; case NONE: showNotification = false; break; } if (pushMessage == null) { Log.d(TAG, "Push message is null."); showNotification = false; } if (showNotification) { // Show a separate notification for each channel. sendAnnouncementNotification(channelId); } } }
/** Load data described by push message. */ private void handlePushMessage() { int messageNumber; NotificationSettings notificationSettings; switch (pushMessage.getPushType()) { case ANNOUNCEMENT_NEW: // Load new announcements from server. messageNumber = new ChannelDatabaseManager(getApplicationContext()) .getMaxMessageNumberAnnouncement(pushMessage.getId1()); ChannelAPI.getInstance(getApplicationContext()) .getAnnouncements(pushMessage.getId1(), messageNumber); break; case CHANNEL_DELETED: // Mark local channel as deleted. int channelId = pushMessage.getId1(); ChannelController.deleteChannel(getApplicationContext(), channelId); // Notify user as if this push message had the priority HIGH. notificationSettings = settingsDBM.getChannelNotificationSettings(channelId); if (notificationSettings == NotificationSettings.GENERAL) { notificationSettings = settingsDBM.getSettings().getNotificationSettings(); } if (!notificationSettings.equals(NotificationSettings.NONE)) { sendChannelDeletedNotification(channelId); } break; case CHANNEL_CHANGED: // Load updated channel data from server. ChannelAPI.getInstance(getApplicationContext()).getChannel(pushMessage.getId1()); break; case MODERATOR_ADDED: case MODERATOR_CHANGED: case MODERATOR_REMOVED: // Refresh responsible moderator data. ChannelAPI.getInstance(this).getResponsibleModerators(pushMessage.getId1()); break; case CONVERSATION_CHANGED: case CONVERSATION_CLOSED: GroupAPI.getInstance(getApplicationContext()) .getConversation(pushMessage.getId1(), pushMessage.getId2()); break; case CONVERSATION_CHANGED_ALL: case CONVERSATION_NEW: GroupAPI.getInstance(getApplicationContext()).getConversations(pushMessage.getId1()); break; case CONVERSATION_MESSAGE_NEW: // Get conversation message data. Request new messages only. messageNumber = new GroupDatabaseManager(getApplicationContext()) .getMaxMessageNumberConversationMessage(pushMessage.getId2()); GroupAPI.getInstance(this) .getConversationMessages(pushMessage.getId1(), pushMessage.getId2(), messageNumber); break; case CONVERSATION_DELETED: notificationSettings = settingsDBM.getGroupNotificationSettings(pushMessage.getId1()); if (notificationSettings == NotificationSettings.GENERAL) { notificationSettings = settingsDBM.getSettings().getNotificationSettings(); } if (!notificationSettings.equals(NotificationSettings.NONE)) { sendConversationNotification(pushMessage.getId1()); } new GroupDatabaseManager(getApplicationContext()).deleteConversation(pushMessage.getId2()); new GroupDatabaseManager(getApplicationContext()) .setGroupNewEvents(pushMessage.getId1(), true); break; case PARTICIPANT_NEW: case PARTICIPANT_CHANGED: case PARTICIPANT_LEFT: case PARTICIPANT_REMOVED: GroupAPI.getInstance(this).getGroupMembers(pushMessage.getId1()); break; case BALLOT_NEW: case BALLOT_CHANGED: case BALLOT_CHANGED_ALL: GroupAPI.getInstance(this).getBallots(pushMessage.getId1()); break; case BALLOT_OPTION_DELETED: case BALLOT_OPTION_ALL: GroupAPI.getInstance(this).getOptions(pushMessage.getId1(), pushMessage.getId2(), true); break; case BALLOT_OPTION_VOTE: case BALLOT_OPTION_VOTE_ALL: // Do nothing. Load new votes when user enters ballot screen. break; case GROUP_CHANGED: GroupAPI.getInstance(this).getGroup(pushMessage.getId1()); break; } }