Skip to content

Commit

Permalink
Proper notifications when in foreground.
Browse files Browse the repository at this point in the history
Now game and press activities handle event aimed at them instead of showing a notification.
  • Loading branch information
zond committed Dec 31, 2016
1 parent 61c3ccf commit 973a98b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 34 deletions.
43 changes: 26 additions & 17 deletions app/src/main/java/se/oort/diplicity/MessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.util.Base64;
import android.util.Log;
Expand All @@ -20,6 +21,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.RunnableFuture;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

Expand All @@ -29,15 +31,17 @@

public class MessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

private Handler handler = new Handler();

public static final String FCM_NOTIFY_ACTION = "se.oort.diplicity.FCMNotify";

public static Set<RetrofitActivity> messageSubscribers = Collections.synchronizedSet(new HashSet<RetrofitActivity>());

public static class DiplicityJSON {
String type;
se.oort.diplicity.apigen.Message message;
PhaseMeta phaseMeta;
String gameID;
public String type;
public se.oort.diplicity.apigen.Message message;
public PhaseMeta phaseMeta;
public String gameID;
}

public static DiplicityJSON decodeDataPayload(String diplicityJSON) {
Expand Down Expand Up @@ -66,20 +70,25 @@ public static DiplicityJSON decodeDataPayload(String diplicityJSON) {
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
DiplicityJSON diplicityJSON = decodeDataPayload(remoteMessage.getData().get("DiplicityJSON"));
Log.d("Diplicity", "Received a " + diplicityJSON.type);
for (RetrofitActivity subscriber : messageSubscribers) {
boolean consumed = subscriber.consumeDiplicityJSON(diplicityJSON);
if (consumed) {
Log.d("Diplicity", "" + subscriber + " consumed this notification");
return;
} else {
Log.d("Diplicity", "" + subscriber + " didn't consume this notification, checking if anyone else wants to");
public void onMessageReceived(final RemoteMessage remoteMessage) {
handler.post(new Runnable() {
@Override
public void run() {
DiplicityJSON diplicityJSON = decodeDataPayload(remoteMessage.getData().get("DiplicityJSON"));
Log.d("Diplicity", "Received a " + diplicityJSON.type);
for (RetrofitActivity subscriber : messageSubscribers) {
boolean consumed = subscriber.consumeDiplicityJSON(diplicityJSON);
if (consumed) {
Log.d("Diplicity", "" + subscriber + " consumed this notification");
return;
} else {
Log.d("Diplicity", "" + subscriber + " didn't consume this notification, checking if anyone else wants to");
}
}
Log.d("Diplicity", "Nobody consumed this notification, popping up a regular notification");
sendNotification(remoteMessage);
}
}
Log.d("Diplicity", "Nobody consumed this notification, popping up a regular notification");
sendNotification(remoteMessage);
});
}

private void sendNotification(RemoteMessage remoteMessage) {
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/se/oort/diplicity/RetrofitActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ public static byte[] serialize(Serializable o) {
public Sendable<Throwable> newProgressAndToastHandler(final ErrorHandler onError, final String progressMessage) {
final ProgressDialog progress = new ProgressDialog(this);
if (progressMessage != null) {
progress.setTitle(progressMessage);
if (!progressMessage.equals("")) {
progress.setTitle(progressMessage);
}
progress.setCancelable(true);
progress.show();
this.progressDialogs.add(progress);
}
progress.setCancelable(true);
progress.show();
this.progressDialogs.add(progress);

return new Sendable<Throwable>() {
@Override
Expand Down Expand Up @@ -202,8 +204,10 @@ public void send(Throwable e) {
}
}
} finally {
RetrofitActivity.this.progressDialogs.remove(progress);
progress.dismiss();
if (progressMessage != null) {
RetrofitActivity.this.progressDialogs.remove(progress);
progress.dismiss();
}
}
}
};
Expand Down
30 changes: 25 additions & 5 deletions app/src/main/java/se/oort/diplicity/game/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import se.oort.diplicity.App;
import se.oort.diplicity.ChannelService;
import se.oort.diplicity.MemberTable;
import se.oort.diplicity.MessagingService;
import se.oort.diplicity.OptionsService;
import se.oort.diplicity.R;
import se.oort.diplicity.RetrofitActivity;
Expand Down Expand Up @@ -101,6 +102,30 @@ public static void startGameActivity(Context context, Game game, PhaseMeta phase
context.startActivity(intent);
}

@Override
protected boolean consumeDiplicityJSON(MessagingService.DiplicityJSON diplicityJSON) {
if (diplicityJSON.type.equals("phase") && diplicityJSON.gameID.equals(game.ID)) {
phases = null;
game.NewestPhaseMeta.set(0, diplicityJSON.phaseMeta);
Toast.makeText(this, R.string.the_game_has_a_new_phase, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

@Override
public void onResume() {
super.onResume();
MessagingService.messageSubscribers.add(this);
draw();
}

@Override
public void onPause() {
MessagingService.messageSubscribers.remove(this);
super.onPause();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Expand All @@ -119,11 +144,6 @@ public int compare(Member member, Member t1) {
}
}

public void onResume() {
super.onResume();
draw();
}

public void nextPhase() {
if (phaseMeta != null && phaseMeta.PhaseOrdinal < game.NewestPhaseMeta.get(0).PhaseOrdinal) {
handleReq(
Expand Down
40 changes: 34 additions & 6 deletions app/src/main/java/se/oort/diplicity/game/PressActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.List;

import se.oort.diplicity.ChannelService;
import se.oort.diplicity.FCMReceiver;
import se.oort.diplicity.MessagingService;
import se.oort.diplicity.R;
import se.oort.diplicity.RetrofitActivity;
import se.oort.diplicity.Sendable;
Expand Down Expand Up @@ -66,6 +68,27 @@ public static void startPressActivity(Context context, Game game, ChannelService
context.startActivity(intent);
}

@Override
protected boolean consumeDiplicityJSON(MessagingService.DiplicityJSON diplicityJSON) {
if (diplicityJSON.type.equals("message") && diplicityJSON.message.GameID.equals(game.ID) && diplicityJSON.message.ChannelMembers.equals(channel.Members)) {
loadMessages(false);
return true;
}
return false;
}

@Override
public void onResume() {
super.onResume();
MessagingService.messageSubscribers.add(this);
}

@Override
public void onPause() {
MessagingService.messageSubscribers.remove(this);
super.onPause();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Expand Down Expand Up @@ -106,18 +129,22 @@ public void onClick(View view) {
@Override
public void send(SingleContainer<Message> messageSingleContainer) {
((EditText) findViewById(R.id.new_message_body)).setText("");
loadMessages();
loadMessages(false);
}
},
getResources().getString(R.string.sending_message));
}
});
}

loadMessages();
loadMessages(true);
}

private void loadMessages() {
private void loadMessages(boolean withProgress) {
String message = null;
if (withProgress) {
message = getResources().getString(R.string.loading_messages);
}
handleReq(
messageService.ListMessages(channel.GameID, TextUtils.join(",", channel.Members)),
new Sendable<MultiContainer<Message>>() {
Expand All @@ -143,16 +170,17 @@ public void send(final MultiContainer<Message> messageMultiContainer) {

((LinearLayout) findViewById(R.id.press_messages)).addView(row);
}
findViewById(R.id.press_layout).invalidate();
findViewById(R.id.press_messages).invalidate();
final NestedScrollView pressScroll = (NestedScrollView) findViewById(R.id.press_scroll);
pressScroll.post(new Runnable() {
@Override
public void run() {
findViewById(R.id.press_layout).invalidate();
findViewById(R.id.press_messages).invalidate();
pressScroll.invalidate();
pressScroll.fullScroll(View.FOCUS_DOWN);
}
});
}
}, getResources().getString(R.string.loading_messages));
}, message);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@
<string name="push_notifications">Push notifications</string>
<string name="push_notifications_to_your_device">Push notifications to your device</string>
<string name="dysfunctional_fcm_service">Dysfunctional FCM service?</string>
<string name="the_game_has_a_new_phase">The game has a new phase.</string>
</resources>

0 comments on commit 973a98b

Please sign in to comment.