From d97682d9f3d6f612993716c3bc35d3015c4e0c07 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 28 Jan 2021 21:39:26 +0100 Subject: [PATCH] fix(android): fire localNotificationReceived event on Android (#217) --- .../LocalNotificationManager.java | 4 ++++ .../LocalNotificationsPlugin.java | 22 +++++++++++++++++++ .../TimedNotificationPublisher.java | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java index 29e52adc3..ca0e36b0a 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java @@ -221,6 +221,10 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo if (localNotification.isScheduled()) { triggerScheduledNotification(buildNotification, localNotification); } else { + try { + JSObject notificationJson = new JSObject(localNotification.getSource()); + LocalNotificationsPlugin.fireReceived(notificationJson); + } catch (JSONException e) {} notificationManager.notify(localNotification.getId(), buildNotification); } } diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationsPlugin.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationsPlugin.java index 481285798..556599ce0 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationsPlugin.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationsPlugin.java @@ -1,10 +1,12 @@ package com.capacitorjs.plugins.localnotifications; import android.content.Intent; +import com.getcapacitor.Bridge; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; +import com.getcapacitor.PluginHandle; import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission; @@ -15,6 +17,7 @@ @CapacitorPlugin(name = "LocalNotifications", permissions = @Permission(strings = {}, alias = "display")) public class LocalNotificationsPlugin extends Plugin { + private static Bridge staticBridge = null; private LocalNotificationManager manager; private NotificationStorage notificationStorage; private NotificationChannelManager notificationChannelManager; @@ -26,6 +29,7 @@ public void load() { manager = new LocalNotificationManager(notificationStorage, getActivity(), getContext(), this.bridge.getConfig()); manager.createNotificationChannel(); notificationChannelManager = new NotificationChannelManager(getActivity()); + staticBridge = this.bridge; } @Override @@ -113,4 +117,22 @@ public void deleteChannel(PluginCall call) { public void listChannels(PluginCall call) { notificationChannelManager.listChannels(call); } + + public static void fireReceived(JSObject notification) { + LocalNotificationsPlugin localNotificationsPlugin = LocalNotificationsPlugin.getLocalNotificationsInstance(); + if (localNotificationsPlugin != null) { + localNotificationsPlugin.notifyListeners("localNotificationReceived", notification, true); + } + } + + public static LocalNotificationsPlugin getLocalNotificationsInstance() { + if (staticBridge != null && staticBridge.getWebView() != null) { + PluginHandle handle = staticBridge.getPlugin("LocalNotifications"); + if (handle == null) { + return null; + } + return (LocalNotificationsPlugin) handle.getInstance(); + } + return null; + } } diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java index a0b983748..e62f9ac33 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java @@ -7,6 +7,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import com.getcapacitor.JSObject; import com.getcapacitor.Logger; import java.text.SimpleDateFormat; import java.util.Date; @@ -31,6 +32,9 @@ public void onReceive(Context context, Intent intent) { if (id == Integer.MIN_VALUE) { Logger.error(Logger.tags("LN"), "No valid id supplied", null); } + NotificationStorage storage = new NotificationStorage(context); + JSObject notificationJson = storage.getSavedNotificationAsJSObject(Integer.toString(id)); + LocalNotificationsPlugin.fireReceived(notificationJson); notificationManager.notify(id, notification); rescheduleNotificationIfNeeded(context, intent, id); }