From f200135d414eac762e69c93cf8b00015796ebe42 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 14 May 2018 14:39:31 +0300 Subject: [PATCH] Notifications improvements --- .../thmmy/mthmmy/model/PostNotification.java | 81 ++++++++++++++++++ .../mthmmy/services/FirebaseService.java | 83 +++++++++++++------ 2 files changed, 140 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java b/app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java new file mode 100644 index 00000000..42985352 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java @@ -0,0 +1,81 @@ +package gr.thmmy.mthmmy.model; + +/** + * Class that defines the model of a post as need in notifications. All member variables are + * declared final (thus no setters are supplied). Class has one constructor and getter methods for + * all variables. + *

PostNotification model is described by its post's id, its topic's id & title and by its poster + *

. + */ +public class PostNotification { + final int postId; + final int topicId; + final String topicTitle; + final String poster; + + // Suppresses default constructor + @SuppressWarnings("unused") + PostNotification() { + this.postId = -1; + this.topicId = -1; + this.topicTitle = null; + this.poster = null; + } + + /** + * Constructor specifying all class variables necessary to summarize this post. All variables + * are declared final, once assigned they cannot change. + * + * @param postId this post's id + * @param topicId this post's topicId + * @param topicTitle this post's topicTitle + * @param poster username of this post's author + */ + public PostNotification(int postId, int topicId, String topicTitle, String poster) { + this.postId = postId; + this.topicId = topicId; + this.topicTitle = topicTitle; + this.poster = poster; + } + + /** + * Gets this post's Id. + * + * @return this post's Id + */ + public int getPostId() { + return postId; + } + + /** + * Gets this post's topicId. + * + * @return this post's topicId + */ + public int getTopicId() { + return topicId; + } + + /** + * Gets this post's topicTitle. + * + * @return this post's topicTitle + */ + public String getTopicTitle() { + return topicTitle; + } + + /** + * Gets username of this post's author. + * + * @return username of this post's author + */ + public String getPoster() { + return poster; + } +} + + + + + diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java b/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java index 9e4549a3..cc58c02a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java @@ -8,65 +8,100 @@ import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; +import android.os.Bundle; import android.support.v4.app.NotificationCompat; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; +import org.json.JSONException; +import org.json.JSONObject; + import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.topic.TopicActivity; +import gr.thmmy.mthmmy.model.PostNotification; 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 { - @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); if (remoteMessage.getData().size() > 0) { - - Timber.i("msg%s", remoteMessage.getData()); - sendNotification("stuff"); - + JSONObject json = new JSONObject(remoteMessage.getData()); + try { + int topicId = Integer.parseInt(json.getString("topicId")); + int postId = Integer.parseInt(json.getString("postId")); + String topicTitle = json.getString("topicTitle"); + String poster = json.getString("poster"); + sendNotification(new PostNotification(postId, topicId, topicTitle, poster)); + + } catch (JSONException e) { + Timber.e(e, "JSON Exception"); + } } - - } + private static final String CHANNEL_ID = "Posts"; + private static final String GROUP_KEY = "PostsGroup"; + private static int requestCode = 0; + /** - * Create and show a simple notification containing the received FCM message. + * Create and show a new post notification. */ - private void sendNotification(String message) { + private void sendNotification(PostNotification postNotification) { + 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(); + extras.putString(BUNDLE_TOPIC_URL, topicUrl); + extras.putString(BUNDLE_TOPIC_TITLE, postNotification.getTopicTitle()); + intent.putExtras(extras); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, + PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode++, intent, PendingIntent.FLAG_ONE_SHOT); - String channelId = "ASdf"; //getString(R.string.default_notification_channel_id); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = - new NotificationCompat.Builder(this, channelId) + new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("FCM Message") - .setContentText(message) + .setContentTitle(postNotification.getTopicTitle()) + .setContentText("by " + postNotification.getPoster()) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); - NotificationManager notificationManager = - (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT < 26) notificationBuilder.setPriority(PRIORITY_HIGH); + + NotificationCompat.Builder summaryNotificationBuilder = null; + if (Build.VERSION.SDK_INT >= 21) { + notificationBuilder.setVibrate(new long[0]); + notificationBuilder.setGroup(GROUP_KEY); + + summaryNotificationBuilder = + new NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(R.mipmap.ic_launcher) + .setGroupSummary(true) + .setGroup(GROUP_KEY) + .setStyle(new NotificationCompat.InboxStyle() + .setSummaryText("New Posts")) + .setSound(defaultSoundUri) + .setContentIntent(pendingIntent); + } + - // Since android Oreo notification channel is needed. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel("Posts", - "New Posts", - NotificationManager.IMPORTANCE_DEFAULT); - notificationManager.createNotificationChannel(channel); + NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + // Since android Oreo notification channel is needed. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Topic Updates", NotificationManager.IMPORTANCE_HIGH); + notificationManager.createNotificationChannel(channel); } - notificationManager.notify(0, notificationBuilder.build()); - } + notificationManager.notify(postNotification.getTopicId(), notificationBuilder.build()); + if (Build.VERSION.SDK_INT >= 21) notificationManager.notify(0, summaryNotificationBuilder.build());} }