Skip to content

Commit

Permalink
fix(android): fire localNotificationReceived event on Android (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jan 28, 2021
1 parent b56e111 commit d97682d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit d97682d

Please sign in to comment.