From 74fdbbe3cca0261a94f94134e57ce94a958e22a7 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 16 Jun 2022 15:04:02 +0200 Subject: [PATCH 1/3] feat: add share and open actions --- .../gotify/service/WebSocketService.java | 24 +++++++++++++++++++ client/build.gradle | 2 ++ 2 files changed, 26 insertions(+) diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java index 658fc912..3873dbb4 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketService.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java @@ -323,6 +323,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()); + } + CharSequence formattedMessage = message; if (Extras.useMarkdown(extras)) { formattedMessage = markwon.toMarkdown(message); diff --git a/client/build.gradle b/client/build.gradle index 564e4d5a..363a25d8 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -100,9 +100,11 @@ ext { junit_version = "4.13" threetenbp_version = "1.4.4" json_fire_version = "1.8.4" + javax_annotation_version = "1.3.2" } dependencies { + compile "javax.annotation:javax.annotation-api:$javax_annotation_version" compile "com.squareup.retrofit2:retrofit:$retrofit_version" compile "com.squareup.retrofit2:converter-scalars:$retrofit_version" compile "com.squareup.retrofit2:converter-gson:$retrofit_version" From 4ccbd9d1410ec69f7598fa158eccd808b730eca6 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 16 Jun 2022 23:11:15 +0200 Subject: [PATCH 2/3] feat: add action callback support --- .../gotify/service/WebSocketService.java | 59 +++++++++++-------- client/build.gradle | 2 + 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java index 3873dbb4..52371462 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketService.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java @@ -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; @@ -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; @@ -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 { @@ -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; diff --git a/client/build.gradle b/client/build.gradle index 363a25d8..1662ee38 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -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" From 7e85b0fd366817e26206afce7cc2bf4e90818199 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Fri, 17 Jun 2022 10:40:44 +0200 Subject: [PATCH 3/3] chore: use lambda --- .../gotify/service/WebSocketService.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/gotify/service/WebSocketService.java b/app/src/main/java/com/github/gotify/service/WebSocketService.java index 52371462..065b64ba 100644 --- a/app/src/main/java/com/github/gotify/service/WebSocketService.java +++ b/app/src/main/java/com/github/gotify/service/WebSocketService.java @@ -338,22 +338,19 @@ private void showNotification( 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); - } - }); - } + Runnable callbackRunnable = () -> { + 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); }