13
13
import org .json .*;
14
14
import java .io .*;
15
15
import java .nio .charset .*;
16
+ import java .util .Arrays ;
17
+ import java .util .HashSet ;
18
+ import java .util .Set ;
16
19
17
20
public class NotificationService extends NotificationListenerService
18
21
{
@@ -31,6 +34,15 @@ public void onDestroy()
31
34
32
35
public void onNotificationPosted (StatusBarNotification sbn )
33
36
{
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
+
34
46
SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (this );
35
47
Resources res = getResources ();
36
48
@@ -69,7 +81,6 @@ public void onNotificationPosted(StatusBarNotification sbn)
69
81
final String endpointUsername = prefs .getString (res .getString (R .string .key_endpointuser ), null );
70
82
final String endpointPassword = prefs .getString (res .getString (R .string .key_endpointpw ), null );
71
83
72
- String packageName = sbn .getPackageName ();
73
84
Notification notification = sbn .getNotification ();
74
85
75
86
Object [] payload ;
@@ -81,6 +92,8 @@ else if (res.getString(R.string.protocol_adtv).equals(protocol))
81
92
payload = getPayloadAdtv (packageName , notification );
82
93
else if (res .getString (R .string .protocol_json ).equals (protocol ))
83
94
payload = getPayloadJson (packageName , notification );
95
+ else if (res .getString (R .string .protocol_gotify ).equals (protocol ))
96
+ payload = getPayloadGotify (packageName , notification );
84
97
else
85
98
payload = null ;
86
99
@@ -93,6 +106,7 @@ else if (res.getString(R.string.protocol_json).equals(protocol))
93
106
Intent i = new Intent (this , HttpTransportService .class );
94
107
i .putExtra (HttpTransportService .EXTRA_URL , endpointUrl );
95
108
i .putExtra (HttpTransportService .EXTRA_AUTH , endpointAuth );
109
+ // TODO add support for custom headers
96
110
if (endpointAuth )
97
111
{
98
112
i .putExtra (HttpTransportService .EXTRA_USERNAME , endpointUsername );
@@ -274,4 +288,29 @@ private final Object[] getPayloadJson(String packageName, Notification notificat
274
288
275
289
return new Object [] { "application/json" , result .toString ().getBytes () };
276
290
}
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
+
277
316
}
0 commit comments