From b396f2afc828241aacb9104c9829f88a050511c4 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Sun, 21 May 2017 22:21:23 -0400 Subject: [PATCH] First notification received successfully from the wryk/tusky-api prototype! --- .../com/keylesspalace/tusky/BaseActivity.java | 2 +- .../tusky/fragment/NotificationsFragment.java | 3 - .../tusky/util/PushNotificationClient.java | 68 ++----------------- 3 files changed, 6 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index da1656fcb..97bbebecc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -174,7 +174,7 @@ public class BaseActivity extends AppCompatActivity { protected void createPushNotificationClient() { // TODO: Switch to ssl:// when TLS support is added. pushNotificationClient = new PushNotificationClient(getApplicationContext(), - "tcp://" + getString(R.string.tusky_api_domain) + ":8000"); + "tcp://" + getString(R.string.tusky_api_domain) + ":1883"); } protected void redirectIfNotLoggedIn() { diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index 234ece7fc..bef04290d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -233,9 +233,6 @@ public class NotificationsFragment extends SFragment implements } else { adapter.update(notifications); } - for (Notification notification : notifications) { - Log.d(TAG, "id: " + notification.id); - } if (notifications.size() == 0 && adapter.getItemCount() == 1) { adapter.setFooterState(NotificationsAdapter.FooterState.EMPTY); } else if (fromId != null) { diff --git a/app/src/main/java/com/keylesspalace/tusky/util/PushNotificationClient.java b/app/src/main/java/com/keylesspalace/tusky/util/PushNotificationClient.java index 259704f8d..1d7741a34 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/PushNotificationClient.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/PushNotificationClient.java @@ -2,18 +2,15 @@ package com.keylesspalace.tusky.util; import android.app.NotificationManager; import android.content.Context; -import android.content.SharedPreferences; import android.support.annotation.NonNull; import android.text.Spanned; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.entity.Notification; import com.keylesspalace.tusky.json.SpannedTypeAdapter; import com.keylesspalace.tusky.json.StringWithEmoji; import com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter; -import com.keylesspalace.tusky.network.MastodonAPI; import org.eclipse.paho.android.service.MqttAndroidClient; import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions; @@ -26,20 +23,9 @@ import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayDeque; import java.util.ArrayList; -import okhttp3.Interceptor; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; - import static android.content.Context.NOTIFICATION_SERVICE; public class PushNotificationClient { @@ -67,7 +53,6 @@ public class PushNotificationClient { } private MqttAndroidClient mqttAndroidClient; - private MastodonAPI mastodonApi; private ArrayDeque queuedActions; private ArrayList subscribedTopics; @@ -148,7 +133,8 @@ public class PushNotificationClient { @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { - Log.e(TAG, "An exception occurred while connecting. " + exception.getMessage()); + Log.e(TAG, "An exception occurred while connecting. " + exception.getMessage() + + " " + exception.getCause()); onConnectionFailure(); } }); @@ -230,59 +216,15 @@ public class PushNotificationClient { } private void onMessageReceived(final Context context, String message) { - String notificationId = message; // TODO: finalize the form the messages will be received - - Log.v(TAG, "Notification received: " + notificationId); - - createMastodonAPI(context); - - mastodonApi.notification(notificationId).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - NotificationMaker.make(context, NOTIFY_ID, response.body()); - } - } - - @Override - public void onFailure(Call call, Throwable t) {} - }); - } - - private void createMastodonAPI(Context context) { - SharedPreferences preferences = context.getSharedPreferences( - context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE); - final String domain = preferences.getString("domain", null); - final String accessToken = preferences.getString("accessToken", null); - - OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder() - .addInterceptor(new Interceptor() { - @Override - public okhttp3.Response intercept(Chain chain) throws IOException { - Request originalRequest = chain.request(); - - Request.Builder builder = originalRequest.newBuilder() - .header("Authorization", String.format("Bearer %s", accessToken)); - - Request newRequest = builder.build(); - - return chain.proceed(newRequest); - } - }) - .build(); + Log.v(TAG, "Notification received: " + message); Gson gson = new GsonBuilder() .registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()) .registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()) .create(); + Notification notification = gson.fromJson(message, Notification.class); - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://" + domain) - .client(okHttpClient) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build(); - - mastodonApi = retrofit.create(MastodonAPI.class); + NotificationMaker.make(context, NOTIFY_ID, notification); } public void clearNotifications(Context context) {