Search in sources :

Example 6 with JSArray

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;
}
Also used : JSONObject(org.json.JSONObject) JSArray(com.getcapacitor.JSArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSObject(com.getcapacitor.JSObject) ParseException(java.text.ParseException)

Example 7 with JSArray

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);
    }
}
Also used : JSONArray(org.json.JSONArray) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject) PluginMethod(com.getcapacitor.PluginMethod)

Example 8 with JSArray

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();
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject)

Example 9 with JSArray

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();
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject)

Example 10 with JSArray

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();
}
Also used : JSONObject(org.json.JSONObject) JSArray(com.getcapacitor.JSArray) ArrayList(java.util.ArrayList) JSObject(com.getcapacitor.JSObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JSObject(com.getcapacitor.JSObject) PluginMethod(com.getcapacitor.PluginMethod)

Aggregations

JSArray (com.getcapacitor.JSArray)20 JSObject (com.getcapacitor.JSObject)14 PluginMethod (com.getcapacitor.PluginMethod)12 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)7 Intent (android.content.Intent)3 ArrayList (java.util.ArrayList)3 NotificationChannel (android.app.NotificationChannel)2 LatLng (com.google.android.libraries.maps.model.LatLng)2 Notification (android.app.Notification)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Parcelable (android.os.Parcelable)1 StatusBarNotification (android.service.notification.StatusBarNotification)1 ActivityCallback (com.getcapacitor.annotation.ActivityCallback)1 PolygonOptions (com.google.android.libraries.maps.model.PolygonOptions)1 PolylineOptions (com.google.android.libraries.maps.model.PolylineOptions)1 ParseException (java.text.ParseException)1 Executor (java.util.concurrent.Executor)1 JSONArray (org.json.JSONArray)1