Skip to content

Commit

Permalink
feat: add action callback support
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Jun 16, 2022
1 parent 74fdbbe commit 4ccbd9d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
59 changes: 35 additions & 24 deletions app/src/main/java/com/github/gotify/service/WebSocketService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.gotify.service;

import static com.github.gotify.api.Callback.call;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
Expand All @@ -12,11 +14,15 @@
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import com.github.gotify.MarkwonFactory;
import com.github.gotify.MissedMessageUtil;
import com.github.gotify.NotificationSupport;
Expand All @@ -32,12 +38,15 @@
import com.github.gotify.messages.Extras;
import com.github.gotify.messages.MessagesActivity;
import com.github.gotify.picasso.PicassoHandler;
import io.noties.markwon.Markwon;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import static com.github.gotify.api.Callback.call;
import cz.msebera.android.httpclient.Header;
import io.noties.markwon.Markwon;

public class WebSocketService extends Service {

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

String actionOpen =
Extras.getNestedValue(String.class, extras, "client::notification", "actions", "open");

if (actionOpen != null) {
Intent actionOpenIntent = new Intent();
actionOpenIntent.setAction(Intent.ACTION_VIEW);
actionOpenIntent.setData(Uri.parse(actionOpen));
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 123, actionOpenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
b.addAction(new NotificationCompat.Action.Builder(null, "open", pendingIntent).build());
}

String actionShare =
Extras.getNestedValue(String.class, extras, "client::notification", "actions", "share");

if (actionShare != null) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, actionShare);
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 124, shareIntent, PendingIntent.FLAG_UPDATE_CURRENT);
b.addAction(new NotificationCompat.Action.Builder(null, "share", pendingIntent).build());
String callbackUrl =
Extras.getNestedValue(
String.class, extras, "client::notification", "actions", "callback");

if (callbackUrl != null) {
Handler callbackHandler = new Handler(Looper.getMainLooper());
Runnable callbackRunnable = new Runnable() {
@Override
public void run() {
AsyncHttpClient client = new AsyncHttpClient();
client.post(callbackUrl, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println("Callback successfully send to: " +callbackUrl);
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
System.out.println("Can't send callback to: "+callbackUrl);
}
});
}
};
callbackHandler.post(callbackRunnable);
}

CharSequence formattedMessage = message;
Expand Down
2 changes: 2 additions & 0 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ ext {
threetenbp_version = "1.4.4"
json_fire_version = "1.8.4"
javax_annotation_version = "1.3.2"
android_async_http = "1.4.9"
}

dependencies {
compile "javax.annotation:javax.annotation-api:$javax_annotation_version"
compile "com.loopj.android:android-async-http:$android_async_http"
compile "com.squareup.retrofit2:retrofit:$retrofit_version"
compile "com.squareup.retrofit2:converter-scalars:$retrofit_version"
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"
Expand Down

0 comments on commit 4ccbd9d

Please sign in to comment.