@ -6,8 +6,6 @@ import android.app.NotificationManager;
import android.app.PendingIntent ;
import android.content.Context ;
import android.content.Intent ;
import android.media.RingtoneManager ;
import android.net.Uri ;
import android.os.Build ;
import android.os.Bundle ;
import android.service.notification.StatusBarNotification ;
@ -26,7 +24,7 @@ import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.PostNotification ;
import timber.log.Timber ;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH ;
import static android.support.v4.app.NotificationCompat.PRIORITY_MAX ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE ;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL ;
@ -49,6 +47,8 @@ public class NotificationService extends FirebaseMessagingService {
String poster = json . getString ( "poster" ) ;
sendNotification ( new PostNotification ( postId , topicId , topicTitle , poster ) ) ;
}
else
Timber . v ( "Notification suppressed (own userID)." ) ;
} catch ( JSONException e ) {
Timber . e ( e , "JSON Exception" ) ;
}
@ -56,17 +56,20 @@ public class NotificationService extends FirebaseMessagingService {
}
private static final String CHANNEL_ID = "Posts" ;
private static final String CHANNEL_NAME = "New Posts" ;
private static final String GROUP_KEY = "PostsGroup" ;
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" ;
private static final String DELETED_MESSAGES_TAG = "DELETED_MESSAGES_TAG" ; //notification tag
/ * *
* Create and show a new post notification .
* /
private void sendNotification ( PostNotification postNotification ) {
Timber . i ( "Creating a notification..." ) ;
String topicUrl = "https://www.thmmy.gr/smf/index.php?topic=" + postNotification . getTopicId ( ) + "." + postNotification . getPostId ( ) ;
Intent intent = new Intent ( this , TopicActivity . class ) ;
Bundle extras = new Bundle ( ) ;
@ -93,19 +96,19 @@ public class NotificationService extends FirebaseMessagingService {
Bundle notificationExtras = new Bundle ( ) ;
notificationExtras . putInt ( NEW_POSTS_COUNT , newPostsCount ) ;
Uri defaultSoundUri = RingtoneManager . getDefaultUri ( RingtoneManager . TYPE_NOTIFICATION ) ;
NotificationCompat . Builder notificationBuilder =
new NotificationCompat . Builder ( this , CHANNEL_ID )
. setSmallIcon ( R . mipmap . ic_launcher )
. setContentTitle ( postNotification . getTopicTitle ( ) )
. setContentText ( contentText )
. setAutoCancel ( true )
. setSound ( defaultSoundUri )
. setContentIntent ( pendingIntent )
. setDefaults ( Notification . DEFAULT_ALL )
. setGroup ( GROUP_KEY )
. addExtras ( notificationExtras ) ;
if ( buildVersion < Build . VERSION_CODES . O )
notificationBuilder . setPriority ( PRIORITY_HIGH ) ;
notificationBuilder . setPriority ( PRIORITY_MAX ) ;
boolean createSummaryNotification = false ;
if ( buildVersion > = Build . VERSION_CODES . LOLLIPOP )
@ -115,9 +118,6 @@ public class NotificationService extends FirebaseMessagingService {
createSummaryNotification = otherNotificationsExist ( topicId ) ;
}
notificationBuilder . setVibrate ( new long [ 0 ] ) ;
notificationBuilder . setGroup ( GROUP_KEY ) ;
NotificationCompat . Builder summaryNotificationBuilder = null ;
if ( createSummaryNotification )
{
@ -129,7 +129,7 @@ public class NotificationService extends FirebaseMessagingService {
. setAutoCancel ( true )
. setStyle ( new NotificationCompat . InboxStyle ( )
. setSummaryText ( "New Posts" ) )
. setSound ( defaultSoundUri ) ;
. setDefaults ( Notification . DEFAULT_ALL ) ;
}
@ -138,15 +138,11 @@ public class NotificationService extends FirebaseMessagingService {
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
// Since Android Oreo notification channel is needed.
if ( buildVersion > = Build . VERSION_CODES . O ) {
NotificationChannel channel = new NotificationChannel ( CHANNEL_ID , "Topic Updates" , NotificationManager . IMPORTANCE_HIGH ) ;
notificationManager . createNotificationChannel ( channel ) ;
}
if ( buildVersion > = Build . VERSION_CODES . O )
notificationManager . createNotificationChannel ( new NotificationChannel ( CHANNEL_ID , CHANNEL_NAME , NotificationManager . IMPORTANCE_HIGH ) ) ;
notificationManager . notify ( NEW_POST_TAG , topicId , notificationBuilder . build ( ) ) ;
if ( createSummaryNotification )
notificationManager . notify ( SUMMARY_TAG , 0 , summaryNotificationBuilder . build ( ) ) ;
}
@ -172,12 +168,35 @@ public class NotificationService extends FirebaseMessagingService {
if ( notificationManager ! = null ) {
StatusBarNotification [ ] barNotifications = notificationManager . getActiveNotifications ( ) ;
for ( StatusBarNotification notification : barNotifications ) {
if ( notification . getTag ( ) . equals ( NEW_POST_TAG ) & & notification . getId ( ) ! = notificationId )
String tag = notification . getTag ( ) ;
if ( tag ! = null & & tag . equals ( NEW_POST_TAG ) & & notification . getId ( ) ! = notificationId )
return true ;
}
}
return false ;
}
@Override
public void onDeletedMessages ( ) {
super . onDeletedMessages ( ) ;
NotificationCompat . Builder notificationBuilder =
new NotificationCompat . Builder ( this , CHANNEL_ID )
. setSmallIcon ( R . mipmap . ic_launcher )
. setContentTitle ( "Error fetching notifications!" )
. setContentText ( "Some notifications may not have arrived successfully either due to" +
"the amount of pending messages (>100) or if the device hasn't come online for more than a month." )
. setAutoCancel ( true )
. setDefaults ( Notification . DEFAULT_ALL ) ;
if ( buildVersion < Build . VERSION_CODES . O )
notificationBuilder . setPriority ( Notification . PRIORITY_MAX ) ;
NotificationManager notificationManager = ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
// Since Android Oreo notification channel is needed.
if ( buildVersion > = Build . VERSION_CODES . O )
notificationManager . createNotificationChannel ( new NotificationChannel ( CHANNEL_ID , CHANNEL_NAME , NotificationManager . IMPORTANCE_HIGH ) ) ;
notificationManager . notify ( DELETED_MESSAGES_TAG , 0 , notificationBuilder . build ( ) ) ;
}
}