use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.
the class LocalNotification method buildNotificationList.
/**
* Build list of the notifications from remote plugin call
*/
public static List<LocalNotification> buildNotificationList(PluginCall call) {
JSArray notificationArray = call.getArray("notifications");
if (notificationArray == null) {
call.reject("Must provide notifications array as notifications option");
return null;
}
List<LocalNotification> resultLocalNotifications = new ArrayList<>(notificationArray.length());
List<JSONObject> notificationsJson;
try {
notificationsJson = notificationArray.toList();
} catch (JSONException e) {
call.reject("Provided notification format is invalid");
return null;
}
for (JSONObject jsonNotification : notificationsJson) {
JSObject notification = null;
try {
notification = JSObject.fromJSONObject(jsonNotification);
} catch (JSONException e) {
call.reject("Invalid JSON object sent to NotificationPlugin", e);
return null;
}
try {
LocalNotification activeLocalNotification = buildNotificationFromJSObject(notification);
resultLocalNotifications.add(activeLocalNotification);
} catch (ParseException e) {
call.reject("Invalid date format sent to Notification plugin", e);
return null;
}
}
return resultLocalNotifications;
}
use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.
the class LocalNotificationsPlugin method schedule.
/**
* Schedule a notification call from JavaScript
* Creates local notification in system.
*/
@PluginMethod
public void schedule(PluginCall call) {
List<LocalNotification> localNotifications = LocalNotification.buildNotificationList(call);
if (localNotifications == null) {
return;
}
JSONArray ids = manager.schedule(call, localNotifications);
if (ids != null) {
notificationStorage.appendNotifications(localNotifications);
JSObject result = new JSObject();
JSArray jsArray = new JSArray();
for (int i = 0; i < ids.length(); i++) {
try {
JSObject notification = new JSObject().put("id", ids.getInt(i));
jsArray.put(notification);
} catch (Exception ex) {
}
}
result.put("notifications", jsArray);
call.resolve(result);
}
}
use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.
the class NotificationChannelManager method listChannels.
public void listChannels(PluginCall call) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
JSArray channels = new JSArray();
for (NotificationChannel notificationChannel : notificationChannels) {
JSObject channel = new JSObject();
channel.put(CHANNEL_ID, notificationChannel.getId());
channel.put(CHANNEL_NAME, notificationChannel.getName());
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
channel.put(CHANNEL_SOUND, notificationChannel.getSound());
channel.put(CHANNEL_VIBRATE, notificationChannel.shouldVibrate());
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
Logger.debug(Logger.tags("NotificationChannel"), "visibility " + notificationChannel.getLockscreenVisibility());
Logger.debug(Logger.tags("NotificationChannel"), "importance " + notificationChannel.getImportance());
channels.put(channel);
}
JSObject result = new JSObject();
result.put("channels", channels);
call.resolve(result);
} else {
call.unavailable();
}
}
use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.
the class NotificationChannelManager method listChannels.
public void listChannels(PluginCall call) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
JSArray channels = new JSArray();
for (NotificationChannel notificationChannel : notificationChannels) {
JSObject channel = new JSObject();
channel.put(CHANNEL_ID, notificationChannel.getId());
channel.put(CHANNEL_NAME, notificationChannel.getName());
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
channel.put(CHANNEL_SOUND, notificationChannel.getSound());
channel.put(CHANNEL_VIBRATE, notificationChannel.shouldVibrate());
channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
Logger.debug(Logger.tags("NotificationChannel"), "visibility " + notificationChannel.getLockscreenVisibility());
Logger.debug(Logger.tags("NotificationChannel"), "importance " + notificationChannel.getImportance());
channels.put(channel);
}
JSObject result = new JSObject();
result.put("channels", channels);
call.resolve(result);
} else {
call.unavailable();
}
}
use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.
the class PushNotificationsPlugin method removeDeliveredNotifications.
@PluginMethod
public void removeDeliveredNotifications(PluginCall call) {
JSArray notifications = call.getArray("notifications");
List<Integer> ids = new ArrayList<>();
try {
for (Object o : notifications.toList()) {
if (o instanceof JSONObject) {
JSObject notif = JSObject.fromJSONObject((JSONObject) o);
Integer id = notif.getInteger("id");
ids.add(id);
} else {
call.reject("Expected notifications to be a list of notification objects");
}
}
} catch (JSONException e) {
call.reject(e.getMessage());
}
for (int id : ids) {
notificationManager.cancel(id);
}
call.resolve();
}
Aggregations