diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/NotificationPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/NotificationPlugin.java index 093640af..2b8f6b0c 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/NotificationPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/NotificationPlugin.java @@ -146,11 +146,8 @@ public class NotificationPlugin extends DoricJavaPlugin { receiver = systemReceivers.get(subscribeId); if (receiver != null) { getDoricContext().getContext().unregisterReceiver(receiver); - promise.resolve(); - } else { - promise.reject(new JavaValue("Cannot find registered receiver:" + subscribeId)); } - + promise.resolve(); } } diff --git a/doric-demo/src/NotificationDemo.ts b/doric-demo/src/NotificationDemo.ts new file mode 100644 index 00000000..c4d66d18 --- /dev/null +++ b/doric-demo/src/NotificationDemo.ts @@ -0,0 +1,70 @@ +import { Group, Panel, navbar, text, gravity, Color, Stack, LayoutSpec, list, NativeCall, listItem, log, vlayout, Gravity, hlayout, Text, scroller, layoutConfig, image, IView, IVLayout, ScaleType, modal, IText, network, navigator, notification } from "doric"; +import { title, label, colors } from "./utils"; + +@Entry +class NotificationDemo extends Panel { + subscribeId?: string + build(rootView: Group): void { + scroller(vlayout([ + title("Notification Demo"), + label('Publish').apply({ + width: 200, + height: 50, + backgroundColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().just(), + onClick: () => { + notification(context).publish({ + biz: "Test", + name: "Demo", + data: { + a: "1", + b: "2", + } + }) + } + } as IText), + label('Subscribe').apply({ + width: 200, + height: 50, + backgroundColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().just(), + onClick: () => { + notification(context).subscribe({ + biz: "Test", + name: "Demo", + callback: (data) => { + modal(context).alert(`Received notification,data is ${JSON.stringify(data)}`) + } + }).then(e => { + this.subscribeId = e + }) + } + } as IText), + label('Unsubscribe').apply({ + width: 200, + height: 50, + backgroundColor: colors[0], + textSize: 30, + textColor: Color.WHITE, + layoutConfig: layoutConfig().just(), + onClick: () => { + if (this.subscribeId) { + notification(context).unsubscribe(this.subscribeId).then(e => { + this.subscribeId = undefined + }) + } + } + } as IText), + ]).apply({ + layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT), + gravity: gravity().center(), + space: 10, + } as IVLayout)).apply({ + layoutConfig: layoutConfig().most(), + }).in(rootView) + } +} \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m index a297c7c3..fc91d458 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m @@ -19,11 +19,26 @@ #import "DoricNotificationPlugin.h" +@interface DoricNotificationPlugin () + +@property(nonatomic, strong) NSMutableDictionary *observers; +@end @implementation DoricNotificationPlugin +- (NSMutableDictionary *)observers { + if (!_observers) { + _observers = [NSMutableDictionary new]; + } + return _observers; +} + - (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise { - NSString *notificationName = [NSString stringWithFormat:@"__doric__%@#%@", dic, promise]; + NSString *biz = dic[@"biz"]; + NSString *name = dic[@"name"]; + if (biz) { + name = [NSString stringWithFormat:@"__doric__%@#%@", biz, name]; + } NSString *data = dic[@"data"]; NSDictionary *dataDic = nil; if (data) { @@ -33,20 +48,45 @@ - (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise { options:NSJSONReadingMutableContainers error:&err]; } - [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dataDic]; + [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:dataDic]; + [promise resolve:nil]; } - - (void)subscribe:(NSDictionary *)dic withPromise:(DoricPromise *)promise { - NSString *notificationName = [NSString stringWithFormat:@"__doric__%@#%@", dic, promise]; + NSString *biz = dic[@"biz"]; + NSString *name = dic[@"name"]; + if (biz) { + name = [NSString stringWithFormat:@"__doric__%@#%@", biz, name]; + } NSString *callbackId = dic[@"callback"]; - [[NSNotificationCenter defaultCenter] addObserverForName:notificationName - object:nil - queue:[NSOperationQueue mainQueue] - usingBlock:^(NSNotification *note) { - - }]; + __weak typeof(self) _self = self; + id observer = [[NSNotificationCenter defaultCenter] + addObserverForName:name + object:nil + queue:[NSOperationQueue mainQueue] + usingBlock:^(NSNotification *note) { + __strong typeof(_self) self = _self; + DoricPromise *currentPromise = [[DoricPromise alloc] initWithContext:self.doricContext callbackId:callbackId]; + [currentPromise resolve:note.userInfo]; + }]; + self.observers[callbackId] = observer; + [promise resolve:callbackId]; } +- (void)unsubscribe:(NSString *)subscribeId withPromise:(DoricPromise *)promise { + id observer = self.observers[subscribeId]; + if (observer) { + [[NSNotificationCenter defaultCenter] removeObserver:observer]; + [self.observers removeObjectForKey:subscribeId]; + } + [promise resolve:nil]; +} -@end \ No newline at end of file +- (void)dealloc { + [self.observers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { + [[NSNotificationCenter defaultCenter] removeObserver:obj]; + }]; + [self.observers removeAllObjects]; +} + +@end diff --git a/doric-js/bundle/doric-lib.js b/doric-js/bundle/doric-lib.js index aae6e2b3..59163525 100644 --- a/doric-js/bundle/doric-lib.js +++ b/doric-js/bundle/doric-lib.js @@ -2095,7 +2095,7 @@ function notification(context) { }, unsubscribe: (subscribeId) => { context.removeFuncById(subscribeId); - return context.notification.unsubscribe({ subscribeId }); + return context.notification.unsubscribe(subscribeId); } }; } diff --git a/doric-js/bundle/doric-vm.js b/doric-js/bundle/doric-vm.js index 0f2827b1..8c4d987f 100644 --- a/doric-js/bundle/doric-vm.js +++ b/doric-js/bundle/doric-vm.js @@ -3554,7 +3554,7 @@ function notification(context) { }, unsubscribe: (subscribeId) => { context.removeFuncById(subscribeId); - return context.notification.unsubscribe({ subscribeId }); + return context.notification.unsubscribe(subscribeId); } }; } diff --git a/doric-js/lib/src/native/notification.js b/doric-js/lib/src/native/notification.js index 5c8acc96..14036cf8 100644 --- a/doric-js/lib/src/native/notification.js +++ b/doric-js/lib/src/native/notification.js @@ -12,7 +12,7 @@ export function notification(context) { }, unsubscribe: (subscribeId) => { context.removeFuncById(subscribeId); - return context.notification.unsubscribe({ subscribeId }); + return context.notification.unsubscribe(subscribeId); } }; } diff --git a/doric-js/src/native/notification.ts b/doric-js/src/native/notification.ts index 1302d9db..9e7038e9 100644 --- a/doric-js/src/native/notification.ts +++ b/doric-js/src/native/notification.ts @@ -28,7 +28,7 @@ export function notification(context: BridgeContext) { }, unsubscribe: (subscribeId: string) => { (context as any).removeFuncById(subscribeId) - return context.notification.unsubscribe({ subscribeId }) + return context.notification.unsubscribe(subscribeId) } } } \ No newline at end of file