@ -1,5 +1,6 @@
package gr.thmmy.mthmmy.services ;
package gr.thmmy.mthmmy.services ;
import android.app.Notification ;
import android.app.NotificationChannel ;
import android.app.NotificationChannel ;
import android.app.NotificationManager ;
import android.app.NotificationManager ;
import android.app.PendingIntent ;
import android.app.PendingIntent ;
@ -9,6 +10,8 @@ import android.media.RingtoneManager;
import android.net.Uri ;
import android.net.Uri ;
import android.os.Build ;
import android.os.Build ;
import android.os.Bundle ;
import android.os.Bundle ;
import android.service.notification.StatusBarNotification ;
import android.support.annotation.RequiresApi ;
import android.support.v4.app.NotificationCompat ;
import android.support.v4.app.NotificationCompat ;
import com.google.firebase.messaging.FirebaseMessagingService ;
import com.google.firebase.messaging.FirebaseMessagingService ;
@ -21,14 +24,15 @@ import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.topic.TopicActivity ;
import gr.thmmy.mthmmy.activities.topic.TopicActivity ;
import gr.thmmy.mthmmy.base.BaseApplication ;
import gr.thmmy.mthmmy.base.BaseApplication ;
import gr.thmmy.mthmmy.model.PostNotification ;
import gr.thmmy.mthmmy.model.PostNotification ;
import gr.thmmy.mthmmy.session.SessionManager ;
import timber.log.Timber ;
import timber.log.Timber ;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH ;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL ;
public class FirebaseService extends FirebaseMessagingService {
public class NotificationService extends FirebaseMessagingService {
private static final int buildVersion = Build . VERSION . SDK_INT ;
@Override
@Override
public void onMessageReceived ( RemoteMessage remoteMessage ) {
public void onMessageReceived ( RemoteMessage remoteMessage ) {
super . onMessageReceived ( remoteMessage ) ;
super . onMessageReceived ( remoteMessage ) ;
@ -55,6 +59,10 @@ public class FirebaseService extends FirebaseMessagingService {
private static final String GROUP_KEY = "PostsGroup" ;
private static final String GROUP_KEY = "PostsGroup" ;
private static int requestCode = 0 ;
private static int requestCode = 0 ;
private static final String NEW_POSTS_COUNT = "newPostsCount" ;
private static final String NEW_POST_TAG = "NEW_POST" ; //notification tag
private static final String SUMMARY_TAG = "SUMMARY" ;
/ * *
/ * *
* Create and show a new post notification .
* Create and show a new post notification .
* /
* /
@ -69,23 +77,50 @@ public class FirebaseService extends FirebaseMessagingService {
PendingIntent pendingIntent = PendingIntent . getActivity ( this , requestCode + + , intent ,
PendingIntent pendingIntent = PendingIntent . getActivity ( this , requestCode + + , intent ,
PendingIntent . FLAG_ONE_SHOT ) ;
PendingIntent . FLAG_ONE_SHOT ) ;
final int topicId = postNotification . getTopicId ( ) ;
String contentText = "New post by " + postNotification . getPoster ( ) ;
int newPostsCount = 1 ;
if ( buildVersion > = Build . VERSION_CODES . M ) {
Notification existingNotification = getActiveNotification ( topicId ) ;
if ( existingNotification ! = null )
{
newPostsCount = existingNotification . extras . getInt ( NEW_POSTS_COUNT ) + 1 ;
contentText = newPostsCount + " new posts" ;
}
}
Bundle notificationExtras = new Bundle ( ) ;
notificationExtras . putInt ( NEW_POSTS_COUNT , newPostsCount ) ;
Uri defaultSoundUri = RingtoneManager . getDefaultUri ( RingtoneManager . TYPE_NOTIFICATION ) ;
Uri defaultSoundUri = RingtoneManager . getDefaultUri ( RingtoneManager . TYPE_NOTIFICATION ) ;
NotificationCompat . Builder notificationBuilder =
NotificationCompat . Builder notificationBuilder =
new NotificationCompat . Builder ( this , CHANNEL_ID )
new NotificationCompat . Builder ( this , CHANNEL_ID )
. setSmallIcon ( R . mipmap . ic_launcher )
. setSmallIcon ( R . mipmap . ic_launcher )
. setContentTitle ( postNotification . getTopicTitle ( ) )
. setContentTitle ( postNotification . getTopicTitle ( ) )
. setContentText ( "by " + postNotification . getPoster ( ) )
. setContentText ( contentText )
. setAutoCancel ( true )
. setAutoCancel ( true )
. setSound ( defaultSoundUri )
. setSound ( defaultSoundUri )
. setContentIntent ( pendingIntent ) ;
. setContentIntent ( pendingIntent )
. addExtras ( notificationExtras ) ;
if ( Build . VERSION . SDK_INT < 26 ) notificationBuilder . setPriority ( PRIORITY_HIGH ) ;
if ( buildVersion < Build . VERSION_CODES . O )
notificationBuilder . setPriority ( PRIORITY_HIGH ) ;
boolean createSummaryNotification = false ;
if ( buildVersion > = Build . VERSION_CODES . LOLLIPOP )
{
createSummaryNotification = true ;
if ( buildVersion > = Build . VERSION_CODES . M )
createSummaryNotification = otherNotificationsExist ( topicId ) ;
}
NotificationCompat . Builder summaryNotificationBuilder = null ;
if ( Build . VERSION . SDK_INT > = 21 ) {
notificationBuilder . setVibrate ( new long [ 0 ] ) ;
notificationBuilder . setVibrate ( new long [ 0 ] ) ;
notificationBuilder . setGroup ( GROUP_KEY ) ;
notificationBuilder . setGroup ( GROUP_KEY ) ;
NotificationCompat . Builder summaryNotificationBuilder = null ;
if ( createSummaryNotification )
{
summaryNotificationBuilder =
summaryNotificationBuilder =
new NotificationCompat . Builder ( this , CHANNEL_ID )
new NotificationCompat . Builder ( this , CHANNEL_ID )
. setSmallIcon ( R . mipmap . ic_launcher )
. setSmallIcon ( R . mipmap . ic_launcher )
@ -94,21 +129,55 @@ public class FirebaseService extends FirebaseMessagingService {
. setAutoCancel ( true )
. setAutoCancel ( true )
. setStyle ( new NotificationCompat . InboxStyle ( )
. setStyle ( new NotificationCompat . InboxStyle ( )
. setSummaryText ( "New Posts" ) )
. setSummaryText ( "New Posts" ) )
. setSound ( defaultSoundUri )
. setSound ( defaultSoundUri ) ;
. setContentIntent ( pendingIntent ) ;
}
}
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
// Since a ndroid Oreo notification channel is needed.
// Since A ndroid Oreo notification channel is needed.
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
if ( buildVersion > = Build . VERSION_CODES . O ) {
NotificationChannel channel = new NotificationChannel ( CHANNEL_ID , "Topic Updates" , NotificationManager . IMPORTANCE_HIGH ) ;
NotificationChannel channel = new NotificationChannel ( CHANNEL_ID , "Topic Updates" , NotificationManager . IMPORTANCE_HIGH ) ;
notificationManager . createNotificationChannel ( channel ) ;
notificationManager . createNotificationChannel ( channel ) ;
}
}
notificationManager . notify ( postNotification . getTopicId ( ) , notificationBuilder . build ( ) ) ;
notificationManager . notify ( NEW_POST_TAG , topicId , notificationBuilder . build ( ) ) ;
if ( Build . VERSION . SDK_INT > = 21 ) notificationManager . notify ( 0 , summaryNotificationBuilder . build ( ) ) ; }
if ( createSummaryNotification )
notificationManager . notify ( SUMMARY_TAG , 0 , summaryNotificationBuilder . build ( ) ) ;
}
@RequiresApi ( api = Build . VERSION_CODES . M )
private Notification getActiveNotification ( int notificationId ) {
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
if ( notificationManager ! = null )
{
StatusBarNotification [ ] barNotifications = notificationManager . getActiveNotifications ( ) ;
for ( StatusBarNotification notification : barNotifications ) {
if ( notification . getId ( ) = = notificationId )
return notification . getNotification ( ) ;
}
}
return null ;
}
@RequiresApi ( api = Build . VERSION_CODES . M )
private boolean otherNotificationsExist ( int notificationId ) {
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
if ( notificationManager ! = null ) {
StatusBarNotification [ ] barNotifications = notificationManager . getActiveNotifications ( ) ;
for ( StatusBarNotification notification : barNotifications ) {
if ( notification . getTag ( ) . equals ( NEW_POST_TAG ) & & notification . getId ( ) ! = notificationId )
return true ;
}
}
return false ;
}
}
}