From 07664a60fdda6da2345a602b5fb75d4fada7faeb Mon Sep 17 00:00:00 2001 From: d4vidi Date: Wed, 23 Nov 2016 20:01:07 +0200 Subject: [PATCH] Handling notification post (local notif style) --- .../core/notification/IPushNotification.java | 6 ++++++ .../core/notification/PushNotification.java | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java index a142f49..46f1015 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/IPushNotification.java @@ -18,5 +18,11 @@ public interface IPushNotification { */ void onOpened(); + /** + * Handle a request to post this notification. + * @return ID to optionally use for notification deletion. + */ + int onPostRequest(); + PushNotificationProps asProps(); } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index 03706c5..5b8224e 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -68,15 +68,20 @@ public class PushNotification implements IPushNotification { PushNotificationsDrawer.get(mContext).onNotificationOpened(); } + @Override + public int onPostRequest() { + return postNotification(); + } + @Override public PushNotificationProps asProps() { return mNotificationProps.copy(); } - protected void postNotification() { + protected int postNotification() { final PendingIntent pendingIntent = getCTAPendingIntent(); final Notification notification = buildNotification(pendingIntent); - postNotification((int) System.currentTimeMillis(), notification); + return postNotification(notification); } protected void digestNotification() { @@ -141,11 +146,21 @@ public class PushNotification implements IPushNotification { .setAutoCancel(true); } + protected int postNotification(Notification notification) { + int id = createNotificationId(notification); + postNotification(id, notification); + return id; + } + protected void postNotification(int id, Notification notification) { final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notification); } + protected int createNotificationId(Notification notification) { + return (int) System.currentTimeMillis(); + } + protected ReactContext getRunningReactContext() { final ReactNativeHost rnHost = ((ReactApplication) mContext.getApplicationContext()).getReactNativeHost(); if (!rnHost.hasInstance()) { -- 2.26.2