Skip to content

Commit cd5c383

Browse files
committed
Add support for gotify protocol (using token as a GET param in Gotify URL)
Add a (presently static) package deny list
1 parent 3423010 commit cd5c383

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

client/src/main/java/net/kzxiv/notify/client/NotificationService.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import org.json.*;
1414
import java.io.*;
1515
import java.nio.charset.*;
16+
import java.util.Arrays;
17+
import java.util.HashSet;
18+
import java.util.Set;
1619

1720
public class NotificationService extends NotificationListenerService
1821
{
@@ -31,6 +34,15 @@ public void onDestroy()
3134

3235
public void onNotificationPosted(StatusBarNotification sbn)
3336
{
37+
// Skip notifications from denied packages
38+
// TODO make user-configurable
39+
final Set<String> packageDenylist = new HashSet<String>(
40+
Arrays.asList("org.fdroid.fdroid", "com.topjohnwu.magisk", "com.aurora.store", "com.android.messaging", "dev.ukanth.ufirewall", "com.android.packageinstaller"));
41+
String packageName = sbn.getPackageName();
42+
if (packageDenylist.contains(packageName)){
43+
return;
44+
}
45+
3446
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
3547
Resources res = getResources();
3648

@@ -69,7 +81,6 @@ public void onNotificationPosted(StatusBarNotification sbn)
6981
final String endpointUsername = prefs.getString(res.getString(R.string.key_endpointuser), null);
7082
final String endpointPassword = prefs.getString(res.getString(R.string.key_endpointpw), null);
7183

72-
String packageName = sbn.getPackageName();
7384
Notification notification = sbn.getNotification();
7485

7586
Object[] payload;
@@ -81,6 +92,8 @@ else if (res.getString(R.string.protocol_adtv).equals(protocol))
8192
payload = getPayloadAdtv(packageName, notification);
8293
else if (res.getString(R.string.protocol_json).equals(protocol))
8394
payload = getPayloadJson(packageName, notification);
95+
else if (res.getString(R.string.protocol_gotify).equals(protocol))
96+
payload = getPayloadGotify(packageName, notification);
8497
else
8598
payload = null;
8699

@@ -93,6 +106,7 @@ else if (res.getString(R.string.protocol_json).equals(protocol))
93106
Intent i = new Intent(this, HttpTransportService.class);
94107
i.putExtra(HttpTransportService.EXTRA_URL, endpointUrl);
95108
i.putExtra(HttpTransportService.EXTRA_AUTH, endpointAuth);
109+
// TODO add support for custom headers
96110
if (endpointAuth)
97111
{
98112
i.putExtra(HttpTransportService.EXTRA_USERNAME, endpointUsername);
@@ -274,4 +288,29 @@ private final Object[] getPayloadJson(String packageName, Notification notificat
274288

275289
return new Object[] { "application/json", result.toString().getBytes() };
276290
}
291+
private final Object[] getPayloadGotify(String packageName, Notification notification)
292+
{
293+
final String title = notification.extras.getString(Notification.EXTRA_TITLE);
294+
final String message = notification.extras.getString(Notification.EXTRA_TEXT);
295+
296+
JSONObject result = new JSONObject();
297+
try
298+
{
299+
// TODO reformat so it looks like so in gotify:
300+
// "package name - notification" (or something)
301+
//
302+
// notification title
303+
// notification content
304+
//
305+
// TODO don't forget to changes spaces to tabs!
306+
result.put("title", "Notifikator");
307+
//result.put("package", packageName);
308+
result.put("message", String.format("%s: %s\n%s", packageName, title, message));
309+
result.put("priority", 5); // TODO make configurable
310+
}
311+
catch (JSONException ex) {}
312+
313+
return new Object[] { "application/json", result.toString().getBytes() };
314+
}
315+
277316
}

client/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
<string name="protocol_kodi_addon">kodi+addon</string>
1616
<string name="protocol_adtv">de.cyberdream.androidtv.notifications</string>
1717
<string name="protocol_json">json</string>
18+
<string name="protocol_gotify">gotify</string>
1819

1920
<string-array name="protocols">
2021
<item>Kodi</item>
2122
<item>Kodi (Addon)</item>
2223
<item>Notifications for Android TV</item>
2324
<item>JSON</item>
25+
<item>Gotify</item>
2426
</string-array>
2527

2628
<string-array name="protocol_names">
2729
<item>@string/protocol_kodi</item>
2830
<item>@string/protocol_kodi_addon</item>
2931
<item>@string/protocol_adtv</item>
3032
<item>@string/protocol_json</item>
33+
<item>@string/protocol_gotify</item>
3134
</string-array>
3235

3336
<string name="general">General</string>

0 commit comments

Comments
 (0)