From 82c496d55cec9f9b899030cd4a8acd6e495beb47 Mon Sep 17 00:00:00 2001 From: dwlrathod Date: Wed, 1 Apr 2020 11:58:32 +0530 Subject: [PATCH 1/6] Multiple notification channel for LocalNotification implemented --- .../plugin/LocalNotifications.java | 21 ++++ .../plugin/PushNotifications.java | 93 +----------------- .../notification/LocalNotification.java | 10 ++ .../LocalNotificationManager.java | 2 +- .../NotificationChannelManager.java | 95 +++++++++++++++++++ core/src/core-plugin-definitions.ts | 10 ++ 6 files changed, 141 insertions(+), 90 deletions(-) create mode 100644 android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index 287c32011..d9c4067b4 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -1,5 +1,7 @@ package com.getcapacitor.plugin; +import android.app.NotificationManager; +import android.content.Context; import android.content.Intent; import com.getcapacitor.JSArray; @@ -12,6 +14,7 @@ import com.getcapacitor.plugin.notification.LocalNotification; import com.getcapacitor.plugin.notification.LocalNotificationManager; import com.getcapacitor.plugin.notification.NotificationAction; +import com.getcapacitor.plugin.notification.NotificationChannelManager; import com.getcapacitor.plugin.notification.NotificationStorage; import org.json.JSONArray; @@ -29,6 +32,7 @@ public class LocalNotifications extends Plugin { private LocalNotificationManager manager; private NotificationStorage notificationStorage; + public NotificationManager notificationManager; public LocalNotifications() { } @@ -39,6 +43,8 @@ public void load() { notificationStorage = new NotificationStorage(getContext()); manager = new LocalNotificationManager(notificationStorage, getActivity()); manager.createNotificationChannel(); + notificationManager = (NotificationManager) getActivity() + .getSystemService(Context.NOTIFICATION_SERVICE); } @Override @@ -118,5 +124,20 @@ public void areEnabled(PluginCall call) { call.success(data); } + @PluginMethod() + public void createChannel(PluginCall call) { + NotificationChannelManager.createChannel(call, getActivity(), notificationManager); + } + + @PluginMethod() + public void deleteChannel(PluginCall call) { + NotificationChannelManager.deleteChannel(call, notificationManager); + } + + @PluginMethod() + public void listChannels(PluginCall call) { + NotificationChannelManager.listChannel(call, notificationManager); + } + } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java index 084c3982c..e9222d804 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java @@ -1,21 +1,14 @@ package com.getcapacitor.plugin; import android.app.Notification; -import android.app.NotificationChannel; import android.app.NotificationManager; -import android.content.ContentResolver; import android.content.Context; import android.content.Intent; -import android.graphics.Color; -import android.media.AudioAttributes; import android.os.Build; import android.os.Bundle; import android.service.notification.StatusBarNotification; -import androidx.core.app.NotificationCompat; import android.net.Uri; -import android.util.Log; - import com.getcapacitor.Bridge; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; @@ -24,6 +17,7 @@ import com.getcapacitor.PluginCall; import com.getcapacitor.PluginHandle; import com.getcapacitor.PluginMethod; +import com.getcapacitor.plugin.notification.NotificationChannelManager; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.iid.FirebaseInstanceId; @@ -35,22 +29,11 @@ import org.json.JSONObject; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; @NativePlugin() public class PushNotifications extends Plugin { - public static String CHANNEL_ID = "id"; - public static String CHANNEL_NAME = "name"; - public static String CHANNEL_DESCRIPTION = "description"; - public static String CHANNEL_IMPORTANCE = "importance"; - public static String CHANNEL_VISIBILITY = "visibility"; - public static String CHANNEL_SOUND = "sound"; - public static String CHANNEL_USE_LIGHTS = "lights"; - public static String CHANNEL_LIGHT_COLOR = "lightColor"; - public static Bridge staticBridge = null; public static RemoteMessage lastMessage = null; public NotificationManager notificationManager; @@ -187,85 +170,17 @@ public void removeAllDeliveredNotifications(PluginCall call) { @PluginMethod() public void createChannel(PluginCall call) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - JSObject channel = new JSObject(); - channel.put(CHANNEL_ID, call.getString(CHANNEL_ID)); - channel.put(CHANNEL_NAME, call.getString(CHANNEL_NAME)); - channel.put(CHANNEL_DESCRIPTION, call.getString(CHANNEL_DESCRIPTION, "")); - channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC)); - channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE)); - channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null)); - channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false)); - channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null)); - createChannel(channel); - call.success(); - } else { - call.unavailable(); - } + NotificationChannelManager.createChannel(call,getContext(), notificationManager); } @PluginMethod() public void deleteChannel(PluginCall call) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - String channelId = call.getString("id"); - notificationManager.deleteNotificationChannel(channelId); - call.success(); - } else { - call.unavailable(); - } + NotificationChannelManager.deleteChannel(call, notificationManager); } @PluginMethod() public void listChannels(PluginCall call) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - List 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_USE_LIGHTS, notificationChannel.shouldShowLights()); - channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor()))); - Log.d(getLogTag(), "visibility " + notificationChannel.getLockscreenVisibility()); - Log.d(getLogTag(), "importance " + notificationChannel.getImportance()); - channels.put(channel); - } - JSObject result = new JSObject(); - result.put("channels", channels); - call.success(result); - } else { - call.unavailable(); - } - } - - private void createChannel(JSObject channel) { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - NotificationChannel notificationChannelChannel = new NotificationChannel(channel.getString(CHANNEL_ID), channel.getString(CHANNEL_NAME), channel.getInteger(CHANNEL_IMPORTANCE)); - notificationChannelChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION)); - notificationChannelChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY)); - notificationChannelChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS)); - String lightColor = channel.getString(CHANNEL_LIGHT_COLOR); - if (lightColor != null) { - try { - notificationChannelChannel.setLightColor(Color.parseColor(lightColor)); - } catch (IllegalArgumentException ex) { - Log.e(getLogTag(), "Invalid color provided for light color."); - } - } - String sound = channel.getString(CHANNEL_SOUND, null); - if (sound != null && !sound.isEmpty()) { - AudioAttributes audioAttributes = new AudioAttributes.Builder() - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .setUsage(AudioAttributes.USAGE_ALARM).build(); - Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getContext().getPackageName() + "/raw/" + sound); - notificationChannelChannel.setSound(soundUri, audioAttributes); - } - notificationManager.createNotificationChannel(notificationChannelChannel); - } + NotificationChannelManager.listChannel(call, notificationManager); } public void sendToken(String token) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java index 302809751..3dec67907 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java @@ -38,6 +38,7 @@ public class LocalNotification { private JSObject extra; private List attachments; private LocalNotificationSchedule schedule; + private String channelId; private String source; @@ -142,6 +143,14 @@ public void setGroupSummary(boolean groupSummary) { this.groupSummary = groupSummary; } + public String getChannelId() { + return channelId; + } + + public void setChannelId(String channelId) { + this.channelId = channelId; + } + /** * Build list of the notifications from remote plugin call */ @@ -180,6 +189,7 @@ public static List buildNotificationList(PluginCall call) { activeLocalNotification.setIconColor(notification.getString("iconColor")); activeLocalNotification.setAttachments(LocalNotificationAttachment.getAttachments(notification)); activeLocalNotification.setGroupSummary(notification.getBoolean("groupSummary", false)); + activeLocalNotification.setChannelId(notification.getString("channelId")); try { activeLocalNotification.setSchedule(new LocalNotificationSchedule(notification)); } catch (ParseException e) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index 202de9d80..e6f9c68ea 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -147,7 +147,7 @@ public JSONArray schedule(PluginCall call, List localNotifica // TODO media style notification support NotificationCompat.MediaStyle // TODO custom small/large icons private void buildNotification(NotificationManagerCompat notificationManager, LocalNotification localNotification, PluginCall call) { - NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, DEFAULT_NOTIFICATION_CHANNEL_ID) + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, localNotification.getChannelId()) .setContentTitle(localNotification.getTitle()) .setContentText(localNotification.getBody()) .setAutoCancel(true) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java new file mode 100644 index 000000000..d492c2791 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java @@ -0,0 +1,95 @@ +package com.getcapacitor.plugin.notification; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.ContentResolver; +import android.content.Context; +import android.graphics.Color; +import android.media.AudioAttributes; +import android.net.Uri; +import android.util.Log; + + +import com.getcapacitor.JSArray; +import com.getcapacitor.JSObject; +import com.getcapacitor.PluginCall; + +import java.util.List; + +public class NotificationChannelManager { + + private static final String TAG = "NotificationChannel: "; + + + private static String CHANNEL_ID = "id"; + private static String CHANNEL_NAME = "name"; + private static String CHANNEL_DESCRIPTION = "description"; + private static String CHANNEL_IMPORTANCE = "importance"; + private static String CHANNEL_VISIBILITY = "visibility"; + private static String CHANNEL_SOUND = "sound"; + private static String CHANNEL_USE_LIGHTS = "lights"; + private static String CHANNEL_LIGHT_COLOR = "lightColor"; + + + public static void createChannel(PluginCall call, Context context, NotificationManager notificationManager) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + android.app.NotificationChannel notificationChannelChannel = new android.app.NotificationChannel(call.getString(CHANNEL_ID), call.getString(CHANNEL_NAME), call.getInt(CHANNEL_IMPORTANCE)); + notificationChannelChannel.setDescription(call.getString(CHANNEL_DESCRIPTION)); + notificationChannelChannel.setLockscreenVisibility(call.getInt(CHANNEL_VISIBILITY)); + notificationChannelChannel.enableLights(call.getBoolean(CHANNEL_USE_LIGHTS)); + String lightColor = call.getString(CHANNEL_LIGHT_COLOR); + if (lightColor != null) { + try { + notificationChannelChannel.setLightColor(Color.parseColor(lightColor)); + } catch (IllegalArgumentException ex) { + call.error("Invalid color provided for light color.", ex); + } + } + String sound = call.getString(CHANNEL_SOUND, null); + if (sound != null && !sound.isEmpty()) { + AudioAttributes audioAttributes = new AudioAttributes.Builder() + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .setUsage(AudioAttributes.USAGE_ALARM).build(); + Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound); + notificationChannelChannel.setSound(soundUri, audioAttributes); + } + notificationManager.createNotificationChannel(notificationChannelChannel); + } + } + + public static void deleteChannel(PluginCall call, NotificationManager notificationManager) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + String channelId = call.getString("id"); + notificationManager.deleteNotificationChannel(channelId); + call.success(); + } else { + call.unavailable(); + } + } + + public static void listChannel(PluginCall call, NotificationManager notificationManager) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + List 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_USE_LIGHTS, notificationChannel.shouldShowLights()); + channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor()))); + Log.d(TAG, "visibility " + notificationChannel.getLockscreenVisibility()); + Log.d(TAG, "importance " + notificationChannel.getImportance()); + channels.put(channel); + } + JSObject result = new JSObject(); + result.put("channels", channels); + call.success(result); + } else { + call.unavailable(); + } + } +} diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 5ad25babc..198e9e0f9 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1101,6 +1101,12 @@ export interface LocalNotification { * (should be used with the `group` property). */ groupSummary?: boolean; + /** + * Android only: set the notification channel on which local notification + * will generate. If channel with the given name does not exist then the + * notification will not fire. + */ + channelId: string; } export interface LocalNotificationSchedule { @@ -1148,6 +1154,10 @@ export interface LocalNotificationsPlugin extends Plugin { * Remove all native listeners for this plugin */ removeAllListeners(): void; + + createChannel(channel: PushNotificationChannel): Promise; + deleteChannel(channel: PushNotificationChannel): Promise; + listChannels(): Promise; } From 747b670dc8d7ae8c29bcce85347cb85528f14ab5 Mon Sep 17 00:00:00 2001 From: dwlrathod Date: Wed, 1 Apr 2020 18:03:40 +0530 Subject: [PATCH 2/6] Notification channel related changes and browser implementation --- .../plugin/LocalNotifications.java | 1 - .../LocalNotificationManager.java | 23 ------------------- .../NotificationChannelManager.java | 17 ++++++++------ core/src/web/local-notifications.ts | 15 ++++++++++-- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index d9c4067b4..786674416 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -42,7 +42,6 @@ public void load() { super.load(); notificationStorage = new NotificationStorage(getContext()); manager = new LocalNotificationManager(notificationStorage, getActivity()); - manager.createNotificationChannel(); notificationManager = (NotificationManager) getActivity() .getSystemService(Context.NOTIFICATION_SERVICE); } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index e6f9c68ea..eec00d81b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -3,13 +3,11 @@ import android.app.Activity; import android.app.AlarmManager; import android.app.Notification; -import android.app.NotificationChannel; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -43,7 +41,6 @@ public class LocalNotificationManager { public static final String NOTIFICATION_IS_REMOVABLE_KEY = "LocalNotificationRepeating"; public static final String REMOTE_INPUT_KEY = "LocalNotificationRemoteInput"; - public static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "default"; private static final String DEFAULT_PRESS_ACTION = "tap"; private Context context; @@ -94,26 +91,6 @@ public JSObject handleNotificationActionPerformed(Intent data, NotificationStora return dataJson; } - /** - * Create notification channel - */ - public void createNotificationChannel() { - // TODO allow to create multiple channels - // Create the NotificationChannel, but only on API 26+ because - // the NotificationChannel class is new and not in the support library - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - CharSequence name = "Default"; - String description = "Default"; - int importance = android.app.NotificationManager.IMPORTANCE_DEFAULT; - NotificationChannel channel = new NotificationChannel(DEFAULT_NOTIFICATION_CHANNEL_ID, name, importance); - channel.setDescription(description); - // Register the channel with the system; you can't change the importance - // or other notification behaviors after this - android.app.NotificationManager notificationManager = context.getSystemService(android.app.NotificationManager.class); - notificationManager.createNotificationChannel(channel); - } - } - @Nullable public JSONArray schedule(PluginCall call, List localNotifications) { JSONArray ids = new JSONArray(); diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java index d492c2791..3deb152e4 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java @@ -33,14 +33,17 @@ public class NotificationChannelManager { public static void createChannel(PluginCall call, Context context, NotificationManager notificationManager) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - android.app.NotificationChannel notificationChannelChannel = new android.app.NotificationChannel(call.getString(CHANNEL_ID), call.getString(CHANNEL_NAME), call.getInt(CHANNEL_IMPORTANCE)); - notificationChannelChannel.setDescription(call.getString(CHANNEL_DESCRIPTION)); - notificationChannelChannel.setLockscreenVisibility(call.getInt(CHANNEL_VISIBILITY)); - notificationChannelChannel.enableLights(call.getBoolean(CHANNEL_USE_LIGHTS)); + NotificationChannel notificationChannel = new NotificationChannel(call.getString(CHANNEL_ID), call.getString(CHANNEL_NAME), call.getInt(CHANNEL_IMPORTANCE)); + if (call.getString(CHANNEL_DESCRIPTION) != null) + notificationChannel.setDescription(call.getString(CHANNEL_DESCRIPTION)); + if (call.getInt(CHANNEL_VISIBILITY) != null) + notificationChannel.setLockscreenVisibility(call.getInt(CHANNEL_VISIBILITY)); + if (call.getBoolean(CHANNEL_USE_LIGHTS) != null) + notificationChannel.enableLights(call.getBoolean(CHANNEL_USE_LIGHTS)); String lightColor = call.getString(CHANNEL_LIGHT_COLOR); if (lightColor != null) { try { - notificationChannelChannel.setLightColor(Color.parseColor(lightColor)); + notificationChannel.setLightColor(Color.parseColor(lightColor)); } catch (IllegalArgumentException ex) { call.error("Invalid color provided for light color.", ex); } @@ -51,9 +54,9 @@ public static void createChannel(PluginCall call, Context context, NotificationM .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM).build(); Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound); - notificationChannelChannel.setSound(soundUri, audioAttributes); + notificationChannel.setSound(soundUri, audioAttributes); } - notificationManager.createNotificationChannel(notificationChannelChannel); + notificationManager.createNotificationChannel(notificationChannel); } } diff --git a/core/src/web/local-notifications.ts b/core/src/web/local-notifications.ts index d22383b15..71b4396fc 100644 --- a/core/src/web/local-notifications.ts +++ b/core/src/web/local-notifications.ts @@ -21,6 +21,17 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif platforms: ['web'] }); } + createChannel(channel: import("../core-plugin-definitions").PushNotificationChannel): Promise { + throw new Error('Feature not available in the browser. ' + channel.id); + } + + deleteChannel(channel: import("../core-plugin-definitions").PushNotificationChannel): Promise { + throw new Error('Feature not available in the browser. ' + channel.id); + } + + listChannels(): Promise { + throw new Error('Feature not available in the browser'); + } sendPending() { const toRemove: LocalNotification[] = []; @@ -103,7 +114,7 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif if (result === 'denied' || result === 'default') { granted = false; } - resolve({granted}); + resolve({ granted }); }); }); } @@ -116,7 +127,7 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif return; } resolve({ - results: [ result ] + results: [result] }); }); }); From 7eeb12e807df436e0f36478bdba9e6c34c7b1d8f Mon Sep 17 00:00:00 2001 From: dwlrathod Date: Thu, 2 Apr 2020 22:52:37 +0530 Subject: [PATCH 3/6] iOS implementation added and other fixes --- .../plugin/LocalNotifications.java | 3 ++- .../plugin/PushNotifications.java | 2 +- .../LocalNotificationManager.java | 27 ++++++++++++++++++- .../NotificationChannelManager.java | 2 +- core/src/core-plugin-definitions.ts | 2 +- core/src/web/local-notifications.ts | 11 +++++--- .../Capacitor/Plugins/DefaultPlugins.m | 3 +++ .../Plugins/LocalNotifications.swift | 12 +++++++++ 8 files changed, 53 insertions(+), 9 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index 786674416..c987afb96 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -42,6 +42,7 @@ public void load() { super.load(); notificationStorage = new NotificationStorage(getContext()); manager = new LocalNotificationManager(notificationStorage, getActivity()); + manager.createNotificationChannel(); notificationManager = (NotificationManager) getActivity() .getSystemService(Context.NOTIFICATION_SERVICE); } @@ -135,7 +136,7 @@ public void deleteChannel(PluginCall call) { @PluginMethod() public void listChannels(PluginCall call) { - NotificationChannelManager.listChannel(call, notificationManager); + NotificationChannelManager.listChannels(call, notificationManager); } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java index e9222d804..df0bffdbe 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java @@ -180,7 +180,7 @@ public void deleteChannel(PluginCall call) { @PluginMethod() public void listChannels(PluginCall call) { - NotificationChannelManager.listChannel(call, notificationManager); + NotificationChannelManager.listChannels(call, notificationManager); } public void sendToken(String token) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index eec00d81b..3a3015b99 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -3,11 +3,13 @@ import android.app.Activity; import android.app.AlarmManager; import android.app.Notification; +import android.app.NotificationChannel; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -41,6 +43,7 @@ public class LocalNotificationManager { public static final String NOTIFICATION_IS_REMOVABLE_KEY = "LocalNotificationRepeating"; public static final String REMOTE_INPUT_KEY = "LocalNotificationRemoteInput"; + public static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "default"; private static final String DEFAULT_PRESS_ACTION = "tap"; private Context context; @@ -91,6 +94,25 @@ public JSObject handleNotificationActionPerformed(Intent data, NotificationStora return dataJson; } + /** + * Create notification channel + */ + public void createNotificationChannel() { + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is new and not in the support library + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CharSequence name = "Default"; + String description = "Default"; + int importance = android.app.NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel channel = new NotificationChannel(DEFAULT_NOTIFICATION_CHANNEL_ID, name, importance); + channel.setDescription(description); + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + android.app.NotificationManager notificationManager = context.getSystemService(android.app.NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } + @Nullable public JSONArray schedule(PluginCall call, List localNotifications) { JSONArray ids = new JSONArray(); @@ -124,7 +146,10 @@ public JSONArray schedule(PluginCall call, List localNotifica // TODO media style notification support NotificationCompat.MediaStyle // TODO custom small/large icons private void buildNotification(NotificationManagerCompat notificationManager, LocalNotification localNotification, PluginCall call) { - NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, localNotification.getChannelId()) + String channelId = DEFAULT_NOTIFICATION_CHANNEL_ID; + if (localNotification.getChannelId() != null) + channelId = localNotification.getChannelId(); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, channelId) .setContentTitle(localNotification.getTitle()) .setContentText(localNotification.getBody()) .setAutoCancel(true) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java index 3deb152e4..67a862c9f 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java @@ -70,7 +70,7 @@ public static void deleteChannel(PluginCall call, NotificationManager notificati } } - public static void listChannel(PluginCall call, NotificationManager notificationManager) { + public static void listChannels(PluginCall call, NotificationManager notificationManager) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { List notificationChannels = notificationManager.getNotificationChannels(); JSArray channels = new JSArray(); diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 198e9e0f9..3d61b8f38 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1106,7 +1106,7 @@ export interface LocalNotification { * will generate. If channel with the given name does not exist then the * notification will not fire. */ - channelId: string; + channelId?: string; } export interface LocalNotificationSchedule { diff --git a/core/src/web/local-notifications.ts b/core/src/web/local-notifications.ts index 71b4396fc..4c433dc33 100644 --- a/core/src/web/local-notifications.ts +++ b/core/src/web/local-notifications.ts @@ -7,7 +7,9 @@ import { LocalNotificationActionType, LocalNotification, LocalNotificationScheduleResult, - NotificationPermissionResponse + NotificationPermissionResponse, + PushNotificationChannel, + PushNotificationChannelList } from '../core-plugin-definitions'; import { PermissionsRequestResult } from '../definitions'; @@ -21,15 +23,16 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif platforms: ['web'] }); } - createChannel(channel: import("../core-plugin-definitions").PushNotificationChannel): Promise { + + createChannel(channel: PushNotificationChannel): Promise { throw new Error('Feature not available in the browser. ' + channel.id); } - deleteChannel(channel: import("../core-plugin-definitions").PushNotificationChannel): Promise { + deleteChannel(channel: PushNotificationChannel): Promise { throw new Error('Feature not available in the browser. ' + channel.id); } - listChannels(): Promise { + listChannels(): Promise { throw new Error('Feature not available in the browser'); } diff --git a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m index 64be5e196..b6d36d6f8 100644 --- a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m +++ b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m @@ -94,6 +94,9 @@ CAP_PLUGIN_METHOD(getPending, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(registerActionTypes, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(areEnabled, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(createChannel, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(deleteChannel, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(listChannels, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone); ) diff --git a/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift b/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift index 0b52f3917..c0032a71c 100644 --- a/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift +++ b/ios/Capacitor/Capacitor/Plugins/LocalNotifications.swift @@ -511,5 +511,17 @@ public class CAPLocalNotificationsPlugin : CAPPlugin { } return opts } + + @objc func createChannel(_ call: CAPPluginCall) { + call.unimplemented() + } + + @objc func deleteChannel(_ call: CAPPluginCall) { + call.unimplemented() + } + + @objc func listChannels(_ call: CAPPluginCall) { + call.unimplemented() + } } From 295bb34421f2c94782ab7c0997a03aab9a959710 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 3 Apr 2020 13:39:35 +0200 Subject: [PATCH 4/6] types changes --- core/src/core-plugin-definitions.ts | 21 ++++++++++----------- core/src/web/local-notifications.ts | 10 +++++----- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 3d61b8f38..9541b6ea6 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -1104,7 +1104,7 @@ export interface LocalNotification { /** * Android only: set the notification channel on which local notification * will generate. If channel with the given name does not exist then the - * notification will not fire. + * notification will not fire. If not provided, it will use the default channel. */ channelId?: string; } @@ -1146,6 +1146,9 @@ export interface LocalNotificationsPlugin extends Plugin { registerActionTypes(options: { types: LocalNotificationActionType[] }): Promise; cancel(pending: LocalNotificationPendingList): Promise; areEnabled(): Promise; + createChannel(channel: NotificationChannel): Promise; + deleteChannel(channel: NotificationChannel): Promise; + listChannels(): Promise; requestPermission(): Promise; addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void): PluginListenerHandle; addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void): PluginListenerHandle; @@ -1154,10 +1157,6 @@ export interface LocalNotificationsPlugin extends Plugin { * Remove all native listeners for this plugin */ removeAllListeners(): void; - - createChannel(channel: PushNotificationChannel): Promise; - deleteChannel(channel: PushNotificationChannel): Promise; - listChannels(): Promise; } @@ -1555,7 +1554,7 @@ export interface PushNotificationDeliveredList { notifications: PushNotification[]; } -export interface PushNotificationChannel { +export interface NotificationChannel { id: string; name: string; description?: string; @@ -1566,8 +1565,8 @@ export interface PushNotificationChannel { lightColor?: string; } -export interface PushNotificationChannelList { - channels: PushNotificationChannel[]; +export interface NotificationChannelList { + channels: NotificationChannel[]; } export interface PushNotificationsPlugin extends Plugin { @@ -1576,9 +1575,9 @@ export interface PushNotificationsPlugin extends Plugin { getDeliveredNotifications(): Promise; removeDeliveredNotifications(delivered: PushNotificationDeliveredList): Promise; removeAllDeliveredNotifications(): Promise; - createChannel(channel: PushNotificationChannel): Promise; - deleteChannel(channel: PushNotificationChannel): Promise; - listChannels(): Promise; + createChannel(channel: NotificationChannel): Promise; + deleteChannel(channel: NotificationChannel): Promise; + listChannels(): Promise; addListener(eventName: 'registration', listenerFunc: (token: PushNotificationToken) => void): PluginListenerHandle; addListener(eventName: 'registrationError', listenerFunc: (error: any) => void): PluginListenerHandle; addListener(eventName: 'pushNotificationReceived', listenerFunc: (notification: PushNotification) => void): PluginListenerHandle; diff --git a/core/src/web/local-notifications.ts b/core/src/web/local-notifications.ts index 4c433dc33..3ce86b4fa 100644 --- a/core/src/web/local-notifications.ts +++ b/core/src/web/local-notifications.ts @@ -8,8 +8,8 @@ import { LocalNotification, LocalNotificationScheduleResult, NotificationPermissionResponse, - PushNotificationChannel, - PushNotificationChannelList + NotificationChannel, + NotificationChannelList } from '../core-plugin-definitions'; import { PermissionsRequestResult } from '../definitions'; @@ -24,15 +24,15 @@ export class LocalNotificationsPluginWeb extends WebPlugin implements LocalNotif }); } - createChannel(channel: PushNotificationChannel): Promise { + createChannel(channel: NotificationChannel): Promise { throw new Error('Feature not available in the browser. ' + channel.id); } - deleteChannel(channel: PushNotificationChannel): Promise { + deleteChannel(channel: NotificationChannel): Promise { throw new Error('Feature not available in the browser. ' + channel.id); } - listChannels(): Promise { + listChannels(): Promise { throw new Error('Feature not available in the browser'); } From 001023a7d77e32282ca3c79fe529a0e907023c68 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 3 Apr 2020 15:36:43 +0200 Subject: [PATCH 5/6] Android fixes --- .../LocalNotificationManager.java | 3 +- .../NotificationChannelManager.java | 40 ++++++++++++++----- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index 3a3015b99..8729a0b98 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -147,8 +147,9 @@ public JSONArray schedule(PluginCall call, List localNotifica // TODO custom small/large icons private void buildNotification(NotificationManagerCompat notificationManager, LocalNotification localNotification, PluginCall call) { String channelId = DEFAULT_NOTIFICATION_CHANNEL_ID; - if (localNotification.getChannelId() != null) + if (localNotification.getChannelId() != null) { channelId = localNotification.getChannelId(); + } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.context, channelId) .setContentTitle(localNotification.getTitle()) .setContentText(localNotification.getBody()) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java index 67a862c9f..fbb6341e5 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java @@ -10,6 +10,8 @@ import android.util.Log; +import androidx.core.app.NotificationCompat; + import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.PluginCall; @@ -30,26 +32,42 @@ public class NotificationChannelManager { private static String CHANNEL_USE_LIGHTS = "lights"; private static String CHANNEL_LIGHT_COLOR = "lightColor"; - public static void createChannel(PluginCall call, Context context, NotificationManager notificationManager) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - NotificationChannel notificationChannel = new NotificationChannel(call.getString(CHANNEL_ID), call.getString(CHANNEL_NAME), call.getInt(CHANNEL_IMPORTANCE)); - if (call.getString(CHANNEL_DESCRIPTION) != null) - notificationChannel.setDescription(call.getString(CHANNEL_DESCRIPTION)); - if (call.getInt(CHANNEL_VISIBILITY) != null) - notificationChannel.setLockscreenVisibility(call.getInt(CHANNEL_VISIBILITY)); - if (call.getBoolean(CHANNEL_USE_LIGHTS) != null) - notificationChannel.enableLights(call.getBoolean(CHANNEL_USE_LIGHTS)); - String lightColor = call.getString(CHANNEL_LIGHT_COLOR); + JSObject channel = new JSObject(); + channel.put(CHANNEL_ID, call.getString(CHANNEL_ID)); + channel.put(CHANNEL_NAME, call.getString(CHANNEL_NAME)); + channel.put(CHANNEL_DESCRIPTION, call.getString(CHANNEL_DESCRIPTION, "")); + channel.put(CHANNEL_VISIBILITY, call.getInt(CHANNEL_VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC)); + channel.put(CHANNEL_IMPORTANCE, call.getInt(CHANNEL_IMPORTANCE)); + channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null)); + channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false)); + channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null)); + createChannel(channel, context, notificationManager); + call.success(); + } else { + call.unavailable(); + } + } + public static void createChannel(JSObject channel, Context context, NotificationManager notificationManager) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + NotificationChannel notificationChannel = new NotificationChannel(channel.getString(CHANNEL_ID), channel.getString(CHANNEL_NAME), channel.getInteger(CHANNEL_IMPORTANCE)); + notificationChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION)); + notificationChannel.setLockscreenVisibility(channel.getInteger(CHANNEL_VISIBILITY)); + notificationChannel.enableLights(channel.getBool(CHANNEL_USE_LIGHTS)); + String lightColor = channel.getString(CHANNEL_LIGHT_COLOR); if (lightColor != null) { try { notificationChannel.setLightColor(Color.parseColor(lightColor)); } catch (IllegalArgumentException ex) { - call.error("Invalid color provided for light color.", ex); + Log.e(TAG, "Invalid color provided for light color."); } } - String sound = call.getString(CHANNEL_SOUND, null); + String sound = channel.getString(CHANNEL_SOUND, null); if (sound != null && !sound.isEmpty()) { + if (sound.contains(".")) { + sound = sound.substring(0, sound.lastIndexOf('.')); + } AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM).build(); From abaf9b91ecec42895b895a4895a3331389563b5b Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 3 Apr 2020 15:58:11 +0200 Subject: [PATCH 6/6] don't use static --- .../plugin/LocalNotifications.java | 13 ++++------- .../plugin/PushNotifications.java | 9 ++++---- .../NotificationChannelManager.java | 23 +++++++++++++++---- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index c987afb96..b015711df 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -1,7 +1,5 @@ package com.getcapacitor.plugin; -import android.app.NotificationManager; -import android.content.Context; import android.content.Intent; import com.getcapacitor.JSArray; @@ -32,7 +30,7 @@ public class LocalNotifications extends Plugin { private LocalNotificationManager manager; private NotificationStorage notificationStorage; - public NotificationManager notificationManager; + private NotificationChannelManager notificationChannelManager; public LocalNotifications() { } @@ -43,8 +41,7 @@ public void load() { notificationStorage = new NotificationStorage(getContext()); manager = new LocalNotificationManager(notificationStorage, getActivity()); manager.createNotificationChannel(); - notificationManager = (NotificationManager) getActivity() - .getSystemService(Context.NOTIFICATION_SERVICE); + notificationChannelManager = new NotificationChannelManager(getActivity()); } @Override @@ -126,17 +123,17 @@ public void areEnabled(PluginCall call) { @PluginMethod() public void createChannel(PluginCall call) { - NotificationChannelManager.createChannel(call, getActivity(), notificationManager); + notificationChannelManager.createChannel(call); } @PluginMethod() public void deleteChannel(PluginCall call) { - NotificationChannelManager.deleteChannel(call, notificationManager); + notificationChannelManager.deleteChannel(call); } @PluginMethod() public void listChannels(PluginCall call) { - NotificationChannelManager.listChannels(call, notificationManager); + notificationChannelManager.listChannels(call); } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java index df0bffdbe..55f95c199 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/PushNotifications.java @@ -37,7 +37,7 @@ public class PushNotifications extends Plugin { public static Bridge staticBridge = null; public static RemoteMessage lastMessage = null; public NotificationManager notificationManager; - + private NotificationChannelManager notificationChannelManager; private static final String EVENT_TOKEN_CHANGE = "registration"; private static final String EVENT_TOKEN_ERROR = "registrationError"; @@ -50,6 +50,7 @@ public void load() { fireNotification(lastMessage); lastMessage = null; } + notificationChannelManager = new NotificationChannelManager(getActivity(), notificationManager); } @Override @@ -170,17 +171,17 @@ public void removeAllDeliveredNotifications(PluginCall call) { @PluginMethod() public void createChannel(PluginCall call) { - NotificationChannelManager.createChannel(call,getContext(), notificationManager); + notificationChannelManager.createChannel(call); } @PluginMethod() public void deleteChannel(PluginCall call) { - NotificationChannelManager.deleteChannel(call, notificationManager); + notificationChannelManager.deleteChannel(call); } @PluginMethod() public void listChannels(PluginCall call) { - NotificationChannelManager.listChannels(call, notificationManager); + notificationChannelManager.listChannels(call); } public void sendToken(String token) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java index fbb6341e5..6a76adf8d 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/NotificationChannelManager.java @@ -20,6 +20,19 @@ public class NotificationChannelManager { + private Context context; + private NotificationManager notificationManager; + + public NotificationChannelManager(Context context) { + this.context = context; + this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + } + + public NotificationChannelManager(Context context, NotificationManager manager) { + this.context = context; + this.notificationManager = manager; + } + private static final String TAG = "NotificationChannel: "; @@ -32,7 +45,7 @@ public class NotificationChannelManager { private static String CHANNEL_USE_LIGHTS = "lights"; private static String CHANNEL_LIGHT_COLOR = "lightColor"; - public static void createChannel(PluginCall call, Context context, NotificationManager notificationManager) { + public void createChannel(PluginCall call) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { JSObject channel = new JSObject(); channel.put(CHANNEL_ID, call.getString(CHANNEL_ID)); @@ -43,13 +56,13 @@ public static void createChannel(PluginCall call, Context context, NotificationM channel.put(CHANNEL_SOUND, call.getString(CHANNEL_SOUND, null)); channel.put(CHANNEL_USE_LIGHTS, call.getBoolean(CHANNEL_USE_LIGHTS, false)); channel.put(CHANNEL_LIGHT_COLOR, call.getString(CHANNEL_LIGHT_COLOR, null)); - createChannel(channel, context, notificationManager); + createChannel(channel); call.success(); } else { call.unavailable(); } } - public static void createChannel(JSObject channel, Context context, NotificationManager notificationManager) { + public void createChannel(JSObject channel) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(channel.getString(CHANNEL_ID), channel.getString(CHANNEL_NAME), channel.getInteger(CHANNEL_IMPORTANCE)); notificationChannel.setDescription(channel.getString(CHANNEL_DESCRIPTION)); @@ -78,7 +91,7 @@ public static void createChannel(JSObject channel, Context context, Notification } } - public static void deleteChannel(PluginCall call, NotificationManager notificationManager) { + public void deleteChannel(PluginCall call) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { String channelId = call.getString("id"); notificationManager.deleteNotificationChannel(channelId); @@ -88,7 +101,7 @@ public static void deleteChannel(PluginCall call, NotificationManager notificati } } - public static void listChannels(PluginCall call, NotificationManager notificationManager) { + public void listChannels(PluginCall call) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { List notificationChannels = notificationManager.getNotificationChannels(); JSArray channels = new JSArray();