Skip to content

Commit 4ccbd9d

Browse files
committed
feat: add action callback support
1 parent 74fdbbe commit 4ccbd9d

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

app/src/main/java/com/github/gotify/service/WebSocketService.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.gotify.service;
22

3+
import static com.github.gotify.api.Callback.call;
4+
35
import android.app.AlarmManager;
46
import android.app.Notification;
57
import android.app.NotificationManager;
@@ -12,11 +14,15 @@
1214
import android.net.ConnectivityManager;
1315
import android.net.Uri;
1416
import android.os.Build;
17+
import android.os.Handler;
1518
import android.os.IBinder;
19+
import android.os.Looper;
20+
1621
import androidx.annotation.Nullable;
1722
import androidx.annotation.RequiresApi;
1823
import androidx.core.app.NotificationCompat;
1924
import androidx.core.content.ContextCompat;
25+
2026
import com.github.gotify.MarkwonFactory;
2127
import com.github.gotify.MissedMessageUtil;
2228
import com.github.gotify.NotificationSupport;
@@ -32,12 +38,15 @@
3238
import com.github.gotify.messages.Extras;
3339
import com.github.gotify.messages.MessagesActivity;
3440
import com.github.gotify.picasso.PicassoHandler;
35-
import io.noties.markwon.Markwon;
41+
import com.loopj.android.http.AsyncHttpClient;
42+
import com.loopj.android.http.AsyncHttpResponseHandler;
43+
3644
import java.util.List;
3745
import java.util.Map;
3846
import java.util.concurrent.atomic.AtomicLong;
3947

40-
import static com.github.gotify.api.Callback.call;
48+
import cz.msebera.android.httpclient.Header;
49+
import io.noties.markwon.Markwon;
4150

4251
public class WebSocketService extends Service {
4352

@@ -323,28 +332,30 @@ private void showNotification(
323332
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
324333
.setContentIntent(contentIntent);
325334

326-
String actionOpen =
327-
Extras.getNestedValue(String.class, extras, "client::notification", "actions", "open");
328-
329-
if (actionOpen != null) {
330-
Intent actionOpenIntent = new Intent();
331-
actionOpenIntent.setAction(Intent.ACTION_VIEW);
332-
actionOpenIntent.setData(Uri.parse(actionOpen));
333-
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 123, actionOpenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
334-
b.addAction(new NotificationCompat.Action.Builder(null, "open", pendingIntent).build());
335-
}
336-
337-
String actionShare =
338-
Extras.getNestedValue(String.class, extras, "client::notification", "actions", "share");
339-
340-
if (actionShare != null) {
341-
Intent sendIntent = new Intent();
342-
sendIntent.setAction(Intent.ACTION_SEND);
343-
sendIntent.putExtra(Intent.EXTRA_TEXT, actionShare);
344-
sendIntent.setType("text/plain");
345-
Intent shareIntent = Intent.createChooser(sendIntent, null);
346-
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 124, shareIntent, PendingIntent.FLAG_UPDATE_CURRENT);
347-
b.addAction(new NotificationCompat.Action.Builder(null, "share", pendingIntent).build());
335+
String callbackUrl =
336+
Extras.getNestedValue(
337+
String.class, extras, "client::notification", "actions", "callback");
338+
339+
if (callbackUrl != null) {
340+
Handler callbackHandler = new Handler(Looper.getMainLooper());
341+
Runnable callbackRunnable = new Runnable() {
342+
@Override
343+
public void run() {
344+
AsyncHttpClient client = new AsyncHttpClient();
345+
client.post(callbackUrl, null, new AsyncHttpResponseHandler() {
346+
@Override
347+
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
348+
System.out.println("Callback successfully send to: " +callbackUrl);
349+
}
350+
351+
@Override
352+
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
353+
System.out.println("Can't send callback to: "+callbackUrl);
354+
}
355+
});
356+
}
357+
};
358+
callbackHandler.post(callbackRunnable);
348359
}
349360

350361
CharSequence formattedMessage = message;

client/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ ext {
101101
threetenbp_version = "1.4.4"
102102
json_fire_version = "1.8.4"
103103
javax_annotation_version = "1.3.2"
104+
android_async_http = "1.4.9"
104105
}
105106

106107
dependencies {
107108
compile "javax.annotation:javax.annotation-api:$javax_annotation_version"
109+
compile "com.loopj.android:android-async-http:$android_async_http"
108110
compile "com.squareup.retrofit2:retrofit:$retrofit_version"
109111
compile "com.squareup.retrofit2:converter-scalars:$retrofit_version"
110112
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"

0 commit comments

Comments
 (0)