Commit c69e62f5 authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement notification click handler

parent 901b6289
package chat.rocket.android.activity; package chat.rocket.android.activity;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import chat.rocket.android.LaunchUtil; import chat.rocket.android.LaunchUtil;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
...@@ -34,6 +36,25 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -34,6 +36,25 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
} }
}; };
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
Intent intent = getIntent();
if (intent != null) {
if (intent.hasExtra("serverConfigId")) {
SharedPreferences.Editor editor = RocketChatCache.get(this).edit();
editor.putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID,
intent.getStringExtra("serverConfigId"));
if (intent.hasExtra("roomId")) {
editor.putString(RocketChatCache.KEY_SELECTED_ROOM_ID, intent.getStringExtra("roomId"));
}
editor.apply();
}
}
}
}
private void updateServerConfigIdIfNeeded(SharedPreferences prefs) { private void updateServerConfigIdIfNeeded(SharedPreferences prefs) {
String newServerConfigId = prefs.getString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, null); String newServerConfigId = prefs.getString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, null);
if (serverConfigId == null) { if (serverConfigId == null) {
......
package chat.rocket.android.notification; package chat.rocket.android.notification;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import bolts.Task; import bolts.Task;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.activity.MainActivity;
import chat.rocket.android.helper.Avatar; import chat.rocket.android.helper.Avatar;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmStore;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
...@@ -71,12 +77,28 @@ public class StreamNotifyUserNotifier implements Notifier { ...@@ -71,12 +77,28 @@ public class StreamNotifyUserNotifier implements Notifier {
} }
private Notification generateNotification(Bitmap largeIcon) { private Notification generateNotification(Bitmap largeIcon) {
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ServerConfig config = RealmStore.getDefault().executeTransactionForRead(realm ->
realm.where(ServerConfig.class).equalTo("hostname", hostname).findFirst());
if (config != null) {
intent.putExtra("serverConfigId", config.getServerConfigId());
try {
intent.putExtra("roomId", payload.getString("rid"));
} catch (JSONException exception) {
}
}
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),
(int) (System.currentTimeMillis() % Integer.MAX_VALUE),
intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context) NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title) .setContentTitle(title)
.setContentText(text) .setContentText(text)
.setAutoCancel(true) .setAutoCancel(true)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.rocket_chat_notification_24dp); .setSmallIcon(R.drawable.rocket_chat_notification_24dp)
.setContentIntent(pendingIntent);
if (largeIcon != null) { if (largeIcon != null) {
builder.setLargeIcon(largeIcon); builder.setLargeIcon(largeIcon);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment