From 9fa70b4853e237d364d6c98cbe38167ca656da45 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 6 Jun 2018 21:38:55 +0300 Subject: [PATCH] Notifications improvements, small doc changes --- CONTRIBUTING.md | 16 +-- README.md | 1 - app/build.gradle | 18 +--- app/src/main/AndroidManifest.xml | 2 +- ...eService.java => NotificationService.java} | 99 ++++++++++++++++--- 5 files changed, 100 insertions(+), 36 deletions(-) rename app/src/main/java/gr/thmmy/mthmmy/services/{FirebaseService.java => NotificationService.java} (54%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9901c71c..f04586a0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,15 +15,19 @@ There are many ways of contributing to mTHMMY: - Simply using the [latest release version][google-play] from Google Play (anonymous reports are sent automatically) - Joining our [Discord server][discord-server] -- Submitting bugs and ideas on our [Trello board][trello-board] +- Submitting bugs and ideas to our [issue tracker][github-issues] - Forking mTHMMY and submitting [pull requests](#pull-requests) - Joining our core team - Contacting us by email at `thmmynolife@gmail.com` ## Issue tracker -The [mTHMMY Board on trello.com][trello-board] is used as an Issue Tracker, for bugs and improvements concerning the available mTHMMY releases. -Before creating a card to submit an issue please **search the board** for similar entries. +For bugs and improvements we use [GitHub’s issue tracking][github-issues]. +Before creating a new issue make sure to **search the tracker** for similar ones. + +## Compiling + +Due to the app's integration with Firebase, a `google-services.json` is required inside the `app` directory. To get one, either [set up your own Firebase project][firebase-console] (with or without a self hosted [backend][sisyphus], or ask us to provide you the one we use for development. ## Pull requests @@ -38,8 +42,8 @@ follow the workflow below to make a pull request: 1. Fill the PR description with a brief motive for your change and the method you used to achieve it 1. Submit the PR. - - [google-play]: https://play.google.com/store/apps/details?id=gr.thmmy.mthmmy -[trello-board]: https://trello.com/invite/b/4MVlkrkg/44a931707bd0b84a5e0bdfc42b9ae4f1/mthmmy +[github-issues]: https://github.com/ThmmyNoLife/mTHMMY/issues [discord-server]: https://discord.gg/CVt3yrn +[sisyphus]: https://github.com/ThmmyNoLife/Sisyphus +[firebase-console]: https://console.firebase.google.com/ diff --git a/README.md b/README.md index caa249d7..c69fe09d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![API](https://img.shields.io/badge/API-19%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=19) [![Discord Channel](https://img.shields.io/badge/discord-public@mTHMMY-738bd7.svg?style=flat)][discord-server] -[![Trello Board](https://img.shields.io/badge/trello-mTHMMY-red.svg?style=flat)][trello-board] mTHMMY is a mobile app for the [thmmy.gr](https://www.thmmy.gr) community. diff --git a/app/build.gradle b/app/build.gradle index 83430d97..d8a01d0d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,8 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'io.fabric' -if (buildInRelease()) { - apply plugin: 'io.fabric' -} android { compileSdkVersion 27 @@ -37,9 +35,9 @@ dependencies { implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:recyclerview-v7:27.1.1' - implementation 'com.google.firebase:firebase-core:15.0.2' - implementation 'com.google.firebase:firebase-messaging:15.0.2' - implementation 'com.crashlytics.sdk.android:crashlytics:2.9.2' + implementation 'com.google.firebase:firebase-core:16.0.0' + implementation 'com.google.firebase:firebase-messaging:17.0.0' + implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' @@ -57,10 +55,4 @@ dependencies { implementation 'com.jakewharton.timber:timber:4.7.0' } -if (buildInRelease()) { - apply plugin: 'com.google.gms.google-services' -} - -def buildInRelease() { - return getGradle().getStartParameter().getTaskRequests().toString().contains("Release") -} +apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 49b5d305..98f8b187 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -120,7 +120,7 @@ android:name=".services.DownloadService" android:exported="false" /> diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java similarity index 54% rename from app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java rename to app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java index b6fcda79..64e8e1a2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java @@ -1,5 +1,6 @@ package gr.thmmy.mthmmy.services; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; @@ -9,6 +10,8 @@ import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.service.notification.StatusBarNotification; +import android.support.annotation.RequiresApi; import android.support.v4.app.NotificationCompat; 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.base.BaseApplication; import gr.thmmy.mthmmy.model.PostNotification; -import gr.thmmy.mthmmy.session.SessionManager; import timber.log.Timber; 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_URL; -public class FirebaseService extends FirebaseMessagingService { +public class NotificationService extends FirebaseMessagingService { + private static final int buildVersion = Build.VERSION.SDK_INT; + @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); @@ -55,6 +59,10 @@ public class FirebaseService extends FirebaseMessagingService { 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"; + /** * Create and show a new post notification. */ @@ -69,23 +77,50 @@ public class FirebaseService extends FirebaseMessagingService { PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode++, intent, 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); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(postNotification.getTopicTitle()) - .setContentText("by " + postNotification.getPoster()) + .setContentText(contentText) .setAutoCancel(true) .setSound(defaultSoundUri) - .setContentIntent(pendingIntent); + .setContentIntent(pendingIntent) + .addExtras(notificationExtras); + + 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); + } - if (Build.VERSION.SDK_INT < 26) notificationBuilder.setPriority(PRIORITY_HIGH); + notificationBuilder.setVibrate(new long[0]); + notificationBuilder.setGroup(GROUP_KEY); NotificationCompat.Builder summaryNotificationBuilder = null; - if (Build.VERSION.SDK_INT >= 21) { - notificationBuilder.setVibrate(new long[0]); - notificationBuilder.setGroup(GROUP_KEY); - + if(createSummaryNotification) + { summaryNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) @@ -94,21 +129,55 @@ public class FirebaseService extends FirebaseMessagingService { .setAutoCancel(true) .setStyle(new NotificationCompat.InboxStyle() .setSummaryText("New Posts")) - .setSound(defaultSoundUri) - .setContentIntent(pendingIntent); + .setSound(defaultSoundUri); } + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - // Since android Oreo notification channel is needed. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // 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); } - notificationManager.notify(postNotification.getTopicId(), notificationBuilder.build()); - if (Build.VERSION.SDK_INT >= 21) notificationManager.notify(0, summaryNotificationBuilder.build());} + notificationManager.notify(NEW_POST_TAG, topicId, notificationBuilder.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; + } + + }