add Notification demo,and pass
This commit is contained in:
parent
c5a3f205b4
commit
88d45c2967
@ -146,11 +146,8 @@ public class NotificationPlugin extends DoricJavaPlugin {
|
|||||||
receiver = systemReceivers.get(subscribeId);
|
receiver = systemReceivers.get(subscribeId);
|
||||||
if (receiver != null) {
|
if (receiver != null) {
|
||||||
getDoricContext().getContext().unregisterReceiver(receiver);
|
getDoricContext().getContext().unregisterReceiver(receiver);
|
||||||
promise.resolve();
|
|
||||||
} else {
|
|
||||||
promise.reject(new JavaValue("Cannot find registered receiver:" + subscribeId));
|
|
||||||
}
|
}
|
||||||
|
promise.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
doric-demo/src/NotificationDemo.ts
Normal file
70
doric-demo/src/NotificationDemo.ts
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -19,11 +19,26 @@
|
|||||||
|
|
||||||
#import "DoricNotificationPlugin.h"
|
#import "DoricNotificationPlugin.h"
|
||||||
|
|
||||||
|
@interface DoricNotificationPlugin ()
|
||||||
|
|
||||||
|
@property(nonatomic, strong) NSMutableDictionary<NSString *, id> *observers;
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation DoricNotificationPlugin
|
@implementation DoricNotificationPlugin
|
||||||
|
|
||||||
|
- (NSMutableDictionary *)observers {
|
||||||
|
if (!_observers) {
|
||||||
|
_observers = [NSMutableDictionary new];
|
||||||
|
}
|
||||||
|
return _observers;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
- (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"];
|
NSString *data = dic[@"data"];
|
||||||
NSDictionary *dataDic = nil;
|
NSDictionary *dataDic = nil;
|
||||||
if (data) {
|
if (data) {
|
||||||
@ -33,20 +48,45 @@ - (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
|||||||
options:NSJSONReadingMutableContainers
|
options:NSJSONReadingMutableContainers
|
||||||
error:&err];
|
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 {
|
- (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"];
|
NSString *callbackId = dic[@"callback"];
|
||||||
[[NSNotificationCenter defaultCenter] addObserverForName:notificationName
|
__weak typeof(self) _self = self;
|
||||||
object:nil
|
id observer = [[NSNotificationCenter defaultCenter]
|
||||||
queue:[NSOperationQueue mainQueue]
|
addObserverForName:name
|
||||||
usingBlock:^(NSNotification *note) {
|
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
|
- (void)dealloc {
|
||||||
|
[self.observers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
|
||||||
|
[[NSNotificationCenter defaultCenter] removeObserver:obj];
|
||||||
|
}];
|
||||||
|
[self.observers removeAllObjects];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
@ -2095,7 +2095,7 @@ function notification(context) {
|
|||||||
},
|
},
|
||||||
unsubscribe: (subscribeId) => {
|
unsubscribe: (subscribeId) => {
|
||||||
context.removeFuncById(subscribeId);
|
context.removeFuncById(subscribeId);
|
||||||
return context.notification.unsubscribe({ subscribeId });
|
return context.notification.unsubscribe(subscribeId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -3554,7 +3554,7 @@ function notification(context) {
|
|||||||
},
|
},
|
||||||
unsubscribe: (subscribeId) => {
|
unsubscribe: (subscribeId) => {
|
||||||
context.removeFuncById(subscribeId);
|
context.removeFuncById(subscribeId);
|
||||||
return context.notification.unsubscribe({ subscribeId });
|
return context.notification.unsubscribe(subscribeId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ export function notification(context) {
|
|||||||
},
|
},
|
||||||
unsubscribe: (subscribeId) => {
|
unsubscribe: (subscribeId) => {
|
||||||
context.removeFuncById(subscribeId);
|
context.removeFuncById(subscribeId);
|
||||||
return context.notification.unsubscribe({ subscribeId });
|
return context.notification.unsubscribe(subscribeId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ export function notification(context: BridgeContext) {
|
|||||||
},
|
},
|
||||||
unsubscribe: (subscribeId: string) => {
|
unsubscribe: (subscribeId: string) => {
|
||||||
(context as any).removeFuncById(subscribeId)
|
(context as any).removeFuncById(subscribeId)
|
||||||
return context.notification.unsubscribe({ subscribeId })
|
return context.notification.unsubscribe(subscribeId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user