From ffe2d2c0369bc61bc833b52423ccb33fa8a87b51 Mon Sep 17 00:00:00 2001 From: Martin Bruse Date: Wed, 28 Dec 2016 10:15:10 +0100 Subject: [PATCH] Reversed the chat view to be bottom instead of top posting. Added a few FCM notification classes, nothing functional yet. --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 17 ++++ .../diplicity/FirebaseMessageIDService.java | 15 +++ .../diplicity/FirebaseMessagingService.java | 70 ++++++++++++++ .../se/oort/diplicity/game/PressActivity.java | 95 +++++++------------ app/src/main/res/layout/content_press.xml | 90 ++++++++++-------- 6 files changed, 185 insertions(+), 103 deletions(-) create mode 100644 app/src/main/java/se/oort/diplicity/FirebaseMessageIDService.java create mode 100644 app/src/main/java/se/oort/diplicity/FirebaseMessagingService.java diff --git a/app/build.gradle b/app/build.gradle index bb9e3f6..ec53ca1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,6 +38,7 @@ dependencies { compile 'com.google.android.gms:play-services:10.0.1' compile 'com.google.firebase:firebase-crash:10.0.1' compile 'com.google.firebase:firebase-core:10.0.1' + compile 'com.google.firebase:firebase-messaging:10.0.1' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6cfb88b..a94993b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/se/oort/diplicity/FirebaseMessageIDService.java b/app/src/main/java/se/oort/diplicity/FirebaseMessageIDService.java new file mode 100644 index 0000000..a47530b --- /dev/null +++ b/app/src/main/java/se/oort/diplicity/FirebaseMessageIDService.java @@ -0,0 +1,15 @@ +package se.oort.diplicity; + +import android.util.Log; + +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.FirebaseInstanceIdService; + +public class FirebaseMessageIDService extends FirebaseInstanceIdService { + + @Override + public void onTokenRefresh() { + String refreshedToken = FirebaseInstanceId.getInstance().getToken(); + Log.d("Diplicity", "Refreshed IID token: " + refreshedToken); + } +} diff --git a/app/src/main/java/se/oort/diplicity/FirebaseMessagingService.java b/app/src/main/java/se/oort/diplicity/FirebaseMessagingService.java new file mode 100644 index 0000000..f76b323 --- /dev/null +++ b/app/src/main/java/se/oort/diplicity/FirebaseMessagingService.java @@ -0,0 +1,70 @@ +package se.oort.diplicity; + +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.media.RingtoneManager; +import android.net.Uri; +import android.support.v4.app.NotificationCompat; +import android.util.Log; +import com.google.firebase.messaging.RemoteMessage; + +public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { + @Override + public void onMessageReceived(RemoteMessage remoteMessage) { + // [START_EXCLUDE] + // There are two types of messages data messages and notification messages. Data messages are handled + // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type + // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app + // is in the foreground. When the app is in the background an automatically generated notification is displayed. + // When the user taps on the notification they are returned to the app. Messages containing both notification + // and data payloads are treated as notification messages. The Firebase console always sends notification + // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options + // [END_EXCLUDE] + + // TODO(developer): Handle FCM messages here. + // Not getting messages here? See why this may be: https://goo.gl/39bRNJ + Log.d("Diplicity", "From: " + remoteMessage.getFrom()); + + // Check if message contains a data payload. + if (remoteMessage.getData().size() > 0) { + Log.d("Diplicity", "Message data payload: " + remoteMessage.getData()); + } + + // Check if message contains a notification payload. + if (remoteMessage.getNotification() != null) { + Log.d("Diplicity", "Message Notification Body: " + remoteMessage.getNotification().getBody()); + } + + // Also if you intend on generating your own notifications as a result of a received FCM + // message, here is where that should be initiated. See sendNotification method below. + } + // [END receive_message] + + /** + * Create and show a simple notification containing the received FCM message. + * + * @param messageBody FCM message body received. + */ + private void sendNotification(String messageBody) { + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, + PendingIntent.FLAG_ONE_SHOT); + + Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.ic_otto) + .setContentTitle("FCM Message") + .setContentText(messageBody) + .setAutoCancel(true) + .setSound(defaultSoundUri) + .setContentIntent(pendingIntent); + + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); + } +} diff --git a/app/src/main/java/se/oort/diplicity/game/PressActivity.java b/app/src/main/java/se/oort/diplicity/game/PressActivity.java index b3264d3..3f88b8a 100644 --- a/app/src/main/java/se/oort/diplicity/game/PressActivity.java +++ b/app/src/main/java/se/oort/diplicity/game/PressActivity.java @@ -6,7 +6,9 @@ import android.preference.EditTextPreference; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; +import android.support.v4.widget.NestedScrollView; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutCompat; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; @@ -18,7 +20,10 @@ import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.ListView; +import android.widget.RelativeLayout; +import android.widget.ScrollView; import android.widget.TextView; import com.google.gson.Gson; @@ -51,60 +56,6 @@ public class PressActivity extends RetrofitActivity { public Member member; public Game game; - private MessageListAdapter messages = new MessageListAdapter(); - - private class MessageListAdapter extends BaseAdapter { - private List messages = new ArrayList<>(); - - public void add(Message message) { - this.messages.add(message); - notifyDataSetChanged(); - } - - public void replace(List messages) { - this.messages = messages; - notifyDataSetChanged(); - } - - @Override - public int getCount() { - return messages.size(); - } - - @Override - public Object getItem(int i) { - return messages.get(i); - } - - @Override - public long getItemId(int i) { - return i; - } - - @Override - public View getView(int i, View view, ViewGroup viewGroup) { - View row = view; - if (row == null) { - row = getLayoutInflater().inflate(R.layout.message_list_row, viewGroup, false); - } - - String url = null; - for (Member member : game.Members) { - if (member.Nation.equals(messages.get(i).Sender)) { - url = member.User.Picture; - } - } - - ((TextView) row.findViewById(R.id.body)).setText(messages.get(i).Body); - ((TextView) row.findViewById(R.id.at)).setText(messages.get(i).Age.deadlineAt().toString()); - ((TextView) row.findViewById(R.id.sender)).setText(getResources().getString(R.string.x_, messages.get(i).Sender)); - if (url != null) { - PressActivity.this.populateImage((ImageView) row.findViewById(R.id.avatar), url); - } - return row; - } - } - @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); @@ -125,8 +76,6 @@ protected void onPostCreate(Bundle savedInstanceState) { setTitle(TextUtils.join(", ", channel.Members)); - ((ListView) findViewById(R.id.press_messages)).setAdapter(messages); - if (member == null) { findViewById(R.id.send_message_button).setVisibility(View.GONE); findViewById(R.id.body).setVisibility(View.GONE); @@ -164,12 +113,36 @@ private void loadMessages() { new Sendable>() { @Override public void send(final MultiContainer messageMultiContainer) { - final List newMessages= new ArrayList<>(); - for (SingleContainer messageSingleContainer: messageMultiContainer.Properties) { - newMessages.add(messageSingleContainer.Properties); - Log.d("Diplicity", "Got " + messageSingleContainer.Properties.Body); + ((LinearLayout) findViewById(R.id.press_messages)).removeAllViews(); + for (int i = 0; i < messageMultiContainer.Properties.size(); i++) { + Message message = messageMultiContainer.Properties.get(messageMultiContainer.Properties.size() - i - 1).Properties; + View row = getLayoutInflater().inflate(R.layout.message_list_row, (ViewGroup) findViewById(R.id.press_layout), false); + String url = null; + for (Member member : game.Members) { + if (member.Nation.equals(message.Sender)) { + url = member.User.Picture; + } + } + + ((TextView) row.findViewById(R.id.body)).setText(message.Body); + ((TextView) row.findViewById(R.id.at)).setText(message.Age.deadlineAt().toString()); + ((TextView) row.findViewById(R.id.sender)).setText(getResources().getString(R.string.x_, message.Sender)); + if (url != null) { + PressActivity.this.populateImage((ImageView) row.findViewById(R.id.avatar), url); + } + + ((LinearLayout) findViewById(R.id.press_messages)).addView(row); } - messages.replace(newMessages); + 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() { + Log.d("diplicity", " *** scrolling!"); + pressScroll.fullScroll(View.FOCUS_DOWN); + } + }); } }, getResources().getString(R.string.loading_messages)); } diff --git a/app/src/main/res/layout/content_press.xml b/app/src/main/res/layout/content_press.xml index 85407d3..afef6b1 100644 --- a/app/src/main/res/layout/content_press.xml +++ b/app/src/main/res/layout/content_press.xml @@ -1,5 +1,7 @@ - - - - - - - + + + + - + - + + + - - +