First notification received successfully from the wryk/tusky-api prototype!
This commit is contained in:
parent
c90c909ca6
commit
b396f2afc8
|
@ -174,7 +174,7 @@ public class BaseActivity extends AppCompatActivity {
|
||||||
protected void createPushNotificationClient() {
|
protected void createPushNotificationClient() {
|
||||||
// TODO: Switch to ssl:// when TLS support is added.
|
// TODO: Switch to ssl:// when TLS support is added.
|
||||||
pushNotificationClient = new PushNotificationClient(getApplicationContext(),
|
pushNotificationClient = new PushNotificationClient(getApplicationContext(),
|
||||||
"tcp://" + getString(R.string.tusky_api_domain) + ":8000");
|
"tcp://" + getString(R.string.tusky_api_domain) + ":1883");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void redirectIfNotLoggedIn() {
|
protected void redirectIfNotLoggedIn() {
|
||||||
|
|
|
@ -233,9 +233,6 @@ public class NotificationsFragment extends SFragment implements
|
||||||
} else {
|
} else {
|
||||||
adapter.update(notifications);
|
adapter.update(notifications);
|
||||||
}
|
}
|
||||||
for (Notification notification : notifications) {
|
|
||||||
Log.d(TAG, "id: " + notification.id);
|
|
||||||
}
|
|
||||||
if (notifications.size() == 0 && adapter.getItemCount() == 1) {
|
if (notifications.size() == 0 && adapter.getItemCount() == 1) {
|
||||||
adapter.setFooterState(NotificationsAdapter.FooterState.EMPTY);
|
adapter.setFooterState(NotificationsAdapter.FooterState.EMPTY);
|
||||||
} else if (fromId != null) {
|
} else if (fromId != null) {
|
||||||
|
|
|
@ -2,18 +2,15 @@ package com.keylesspalace.tusky.util;
|
||||||
|
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.keylesspalace.tusky.R;
|
|
||||||
import com.keylesspalace.tusky.entity.Notification;
|
import com.keylesspalace.tusky.entity.Notification;
|
||||||
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
||||||
import com.keylesspalace.tusky.json.StringWithEmoji;
|
import com.keylesspalace.tusky.json.StringWithEmoji;
|
||||||
import com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter;
|
import com.keylesspalace.tusky.json.StringWithEmojiTypeAdapter;
|
||||||
import com.keylesspalace.tusky.network.MastodonAPI;
|
|
||||||
|
|
||||||
import org.eclipse.paho.android.service.MqttAndroidClient;
|
import org.eclipse.paho.android.service.MqttAndroidClient;
|
||||||
import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
|
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.MqttException;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
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;
|
import static android.content.Context.NOTIFICATION_SERVICE;
|
||||||
|
|
||||||
public class PushNotificationClient {
|
public class PushNotificationClient {
|
||||||
|
@ -67,7 +53,6 @@ public class PushNotificationClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private MqttAndroidClient mqttAndroidClient;
|
private MqttAndroidClient mqttAndroidClient;
|
||||||
private MastodonAPI mastodonApi;
|
|
||||||
private ArrayDeque<QueuedAction> queuedActions;
|
private ArrayDeque<QueuedAction> queuedActions;
|
||||||
private ArrayList<String> subscribedTopics;
|
private ArrayList<String> subscribedTopics;
|
||||||
|
|
||||||
|
@ -148,7 +133,8 @@ public class PushNotificationClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
|
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();
|
onConnectionFailure();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -230,59 +216,15 @@ public class PushNotificationClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onMessageReceived(final Context context, String message) {
|
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: " + message);
|
||||||
|
|
||||||
Log.v(TAG, "Notification received: " + notificationId);
|
|
||||||
|
|
||||||
createMastodonAPI(context);
|
|
||||||
|
|
||||||
mastodonApi.notification(notificationId).enqueue(new Callback<Notification>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(Call<Notification> call, Response<Notification> response) {
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
NotificationMaker.make(context, NOTIFY_ID, response.body());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<Notification> 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();
|
|
||||||
|
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
||||||
.registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter())
|
.registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter())
|
||||||
.create();
|
.create();
|
||||||
|
Notification notification = gson.fromJson(message, Notification.class);
|
||||||
|
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
NotificationMaker.make(context, NOTIFY_ID, notification);
|
||||||
.baseUrl("https://" + domain)
|
|
||||||
.client(okHttpClient)
|
|
||||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
mastodonApi = retrofit.create(MastodonAPI.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearNotifications(Context context) {
|
public void clearNotifications(Context context) {
|
||||||
|
|
Loading…
Reference in New Issue