Browse Source

Notifications improvements, small doc changes

pull/24/head
Ezerous 7 years ago
parent
commit
9fa70b4853
  1. 16
      CONTRIBUTING.md
  2. 1
      README.md
  3. 16
      app/build.gradle
  4. 2
      app/src/main/AndroidManifest.xml
  5. 95
      app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java

16
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/

1
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.

16
app/build.gradle

@ -1,8 +1,6 @@
apply plugin: 'com.android.application'
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")
}

2
app/src/main/AndroidManifest.xml

@ -120,7 +120,7 @@
android:name=".services.DownloadService"
android:exported="false" />
<service
android:name=".services.FirebaseService"
android:name=".services.NotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>

95
app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java → 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 (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.setGroup(GROUP_KEY);
NotificationCompat.Builder summaryNotificationBuilder = null;
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;
}
}
Loading…
Cancel
Save