Browse Source

Notifications improvements

pull/24/head
Ezerous 7 years ago
parent
commit
f200135d41
  1. 81
      app/src/main/java/gr/thmmy/mthmmy/model/PostNotification.java
  2. 81
      app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java

81
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.
* <p>PostNotification model is described by its post's id, its topic's id & title and by its poster
* </p>.
*/
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;
}
}

81
app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java

@ -8,65 +8,100 @@ import android.content.Intent;
import android.media.RingtoneManager; 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.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage; import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.model.PostNotification;
import timber.log.Timber; 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 FirebaseService extends FirebaseMessagingService {
@Override @Override
public void onMessageReceived(RemoteMessage remoteMessage) { public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage); super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) { if (remoteMessage.getData().size() > 0) {
JSONObject json = new JSONObject(remoteMessage.getData());
Timber.i("msg%s", remoteMessage.getData()); try {
sendNotification("stuff"); 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); 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); 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); PendingIntent.FLAG_ONE_SHOT);
String channelId = "ASdf"; //getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId) new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message") .setContentTitle(postNotification.getTopicTitle())
.setContentText(message) .setContentText("by " + postNotification.getPoster())
.setAutoCancel(true) .setAutoCancel(true)
.setSound(defaultSoundUri) .setSound(defaultSoundUri)
.setContentIntent(pendingIntent); .setContentIntent(pendingIntent);
NotificationManager notificationManager = if (Build.VERSION.SDK_INT < 26) notificationBuilder.setPriority(PRIORITY_HIGH);
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
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);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed. // Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Posts", NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Topic Updates", NotificationManager.IMPORTANCE_HIGH);
"New Posts",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel); 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());}
} }

Loading…
Cancel
Save