Added the complete information of the extras bundle to the JSON object return

Added the complete information of the extras bundle to the JSON object return.
With that I could remove the functions getExtra and getExtraLines.
There was also a problem with getExtraLines, since it only provided the last line, not all the lines.
The keys are still the same as before, since I removed the android.-prefix from all the keys, so there should be no big issue with compability. Except for the lines, but I think it is right how it now works (see above).

Only the package is now fetched by a static parameter. Maybe it makes sense, to even get all the data from StatusBarNotification Object to have even more data to provide.

https://github.com/coconauts/NotificationListener-cordova/pull/9
This commit is contained in:
Dave Umrysh 2021-02-18 19:48:23 +00:00
parent a785d7e184
commit b6c45fe607

View File

@ -10,6 +10,7 @@ import android.util.Log;
import org.apache.cordova.PluginResult;
import android.service.notification.StatusBarNotification;
import android.os.Bundle;
import java.util.Set;
public class NotificationCommands extends CordovaPlugin {
@ -83,11 +84,27 @@ public class NotificationCommands extends CordovaPlugin {
Bundle extras = n.getNotification().extras;
json.put("title", getExtra(extras, "android.title"));
//json.put("title", getExtra(extras, "android.title"));
json.put("package", n.getPackageName());
json.put("text", getExtra(extras,"android.text"));
json.put("textLines", getExtraLines(extras, "android.textLines"));
//json.put("text", getExtra(extras,"android.text"));
//json.put("textLines", getExtraLines(extras, "android.textLines"));
Set<String> keys = extras.keySet();
/* Iterate over all keys of the bundle to give back all the information available */
for (String key : keys) {
try {
String printKey = key;
/* If key has a prefix android., this will be removed. */
if(printKey.indexOf("android.")==0 && printKey.length()>8){
printKey = printKey.substring(8,key.length());
}
// json.put(key, bundle.get(key)); see edit below
json.put(printKey, JSONObject.wrap(extras.get(key)));
} catch(JSONException e) {
Log.d(TAG,e.getMessage());
}
}
return json;
}