From 9dabd3b1dbdf1b29af18b2f4d4f093b51f5399b5 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 9 Jan 2020 09:54:08 +0800 Subject: [PATCH] iOS:notification plugin --- doric-android/app/build.gradle | 2 +- doric-android/doric/build.gradle | 2 +- .../pub/doric/plugin/NotificationPlugin.java | 16 ++++-- doric-iOS/Pod/Classes/DoricRegistry.m | 3 +- .../Classes/Plugin/DoricNotificationPlugin.h | 24 +++++++++ .../Classes/Plugin/DoricNotificationPlugin.m | 52 +++++++++++++++++++ doric-js/index.d.ts | 4 +- doric-js/lib/src/native/notification.d.ts | 4 +- doric-js/src/native/notification.ts | 4 +- 9 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.h create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m diff --git a/doric-android/app/build.gradle b/doric-android/app/build.gradle index 193eeb77..816f1ce7 100644 --- a/doric-android/app/build.gradle +++ b/doric-android/app/build.gradle @@ -32,7 +32,7 @@ dependencies { implementation "pub.doric:devkit:${rootProject.ext.Version}" implementation 'com.github.bumptech.glide:glide:4.10.0' implementation 'com.github.bumptech.glide:annotations:4.10.0' - implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.1' + implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0' diff --git a/doric-android/doric/build.gradle b/doric-android/doric/build.gradle index 8c52e3bc..bcaa630c 100644 --- a/doric-android/doric/build.gradle +++ b/doric-android/doric/build.gradle @@ -42,7 +42,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.1.0' api 'com.github.penfeizhou:jsc4a:0.1.0' implementation 'com.squareup.okhttp3:okhttp:4.2.2' - implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.1' + implementation 'com.github.penfeizhou.android.animation:glide-plugin:1.3.2' implementation 'jp.wasabeef:glide-transformations:4.1.0' implementation 'com.google.code.gson:gson:2.8.6' implementation "com.google.android.material:material:1.0.0" 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 cdf9a200..093640af 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 @@ -55,8 +55,12 @@ public class NotificationPlugin extends DoricJavaPlugin { @DoricMethod public void publish(JSObject args, DoricPromise promise) { - String biz = args.getProperty("biz").asString().value(); String name = args.getProperty("name").asString().value(); + JSValue bizValue = args.getProperty("biz"); + if (bizValue.isString()) { + String biz = bizValue.asString().value(); + name = "__doric__" + biz + "#" + name; + } String data = null; JSValue value = args.getProperty("data"); if (value.isString()) { @@ -67,7 +71,7 @@ public class NotificationPlugin extends DoricJavaPlugin { if (value.isBoolean()) { androidSystem = value.asBoolean().value(); } - Intent intent = new Intent("__doric__" + biz + "#" + name); + Intent intent = new Intent(name); intent.putExtra("__doric_data__", data); if (androidSystem) { getDoricContext().getContext().sendBroadcast(intent); @@ -80,8 +84,12 @@ public class NotificationPlugin extends DoricJavaPlugin { @DoricMethod public void subscribe(JSObject args, DoricPromise promise) { - String biz = args.getProperty("biz").asString().value(); String name = args.getProperty("name").asString().value(); + JSValue bizValue = args.getProperty("biz"); + if (bizValue.isString()) { + String biz = bizValue.asString().value(); + name = "__doric__" + biz + "#" + name; + } final String callbackId = args.getProperty("callback").asString().value(); JSValue value = args.getProperty("androidSystem"); boolean androidSystem = false; @@ -115,7 +123,7 @@ public class NotificationPlugin extends DoricJavaPlugin { } }; IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction("__doric__" + biz + "#" + name); + intentFilter.addAction(name); if (androidSystem) { getDoricContext().getContext().registerReceiver(receiver, intentFilter); systemReceivers.put(callbackId, receiver); diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index e5c9c56b..1d0275cd 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -46,7 +46,7 @@ #import "DoricInputNode.h" #import "DoricDraggableNode.h" #import "DoricLibrary.h" - +#import "DoricNotificationPlugin.h" @interface DoricLibraries : NSObject @property(nonatomic, strong) NSMutableSet *libraries; @@ -109,6 +109,7 @@ - (void)innerRegister { [self registerNativePlugin:DoricNavBarPlugin.class withName:@"navbar"]; [self registerNativePlugin:DoricPopoverPlugin.class withName:@"popover"]; [self registerNativePlugin:DoricAnimatePlugin.class withName:@"animate"]; + [self registerNativePlugin:DoricNotificationPlugin.class withName:@"notification"]; [self registerViewNode:DoricStackNode.class withName:@"Stack"]; [self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"]; diff --git a/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.h b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.h new file mode 100644 index 00000000..a2ad7872 --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.h @@ -0,0 +1,24 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2020/1/8. +// + +#import +#import "DoricNativePlugin.h" + +@interface DoricNotificationPlugin : DoricNativePlugin +@end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m new file mode 100644 index 00000000..a297c7c3 --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricNotificationPlugin.m @@ -0,0 +1,52 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by pengfei.zhou on 2020/1/8. +// + +#import "DoricNotificationPlugin.h" + + +@implementation DoricNotificationPlugin + +- (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise { + NSString *notificationName = [NSString stringWithFormat:@"__doric__%@#%@", dic, promise]; + NSString *data = dic[@"data"]; + NSDictionary *dataDic = nil; + if (data) { + NSData *jsonData = [data dataUsingEncoding:NSUTF8StringEncoding]; + NSError *err; + dataDic = [NSJSONSerialization JSONObjectWithData:jsonData + options:NSJSONReadingMutableContainers + error:&err]; + } + [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dataDic]; +} + + +- (void)subscribe:(NSDictionary *)dic withPromise:(DoricPromise *)promise { + NSString *notificationName = [NSString stringWithFormat:@"__doric__%@#%@", dic, promise]; + NSString *callbackId = dic[@"callback"]; + [[NSNotificationCenter defaultCenter] addObserverForName:notificationName + object:nil + queue:[NSOperationQueue mainQueue] + usingBlock:^(NSNotification *note) { + + }]; +} + + +@end \ No newline at end of file diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index ce64e1d6..f65ceacb 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -797,13 +797,13 @@ declare module 'doric/lib/src/native/notification' { import { BridgeContext } from "doric/lib/src/runtime/global"; export function notification(context: BridgeContext): { publish: (args: { - biz: string; + biz?: string | undefined; name: string; data?: object | undefined; androidSystem?: boolean | undefined; }) => Promise; subscribe: (args: { - biz: string; + biz?: string | undefined; name: string; callback: (data?: any) => void; androidSystem?: boolean | undefined; diff --git a/doric-js/lib/src/native/notification.d.ts b/doric-js/lib/src/native/notification.d.ts index 4d96750c..68f40fd7 100644 --- a/doric-js/lib/src/native/notification.d.ts +++ b/doric-js/lib/src/native/notification.d.ts @@ -1,13 +1,13 @@ import { BridgeContext } from "../runtime/global"; export declare function notification(context: BridgeContext): { publish: (args: { - biz: string; + biz?: string | undefined; name: string; data?: object | undefined; androidSystem?: boolean | undefined; }) => Promise; subscribe: (args: { - biz: string; + biz?: string | undefined; name: string; callback: (data?: any) => void; androidSystem?: boolean | undefined; diff --git a/doric-js/src/native/notification.ts b/doric-js/src/native/notification.ts index 0c8f522f..1302d9db 100644 --- a/doric-js/src/native/notification.ts +++ b/doric-js/src/native/notification.ts @@ -16,13 +16,13 @@ import { BridgeContext } from "../runtime/global" export function notification(context: BridgeContext) { return { - publish: (args: { biz: string, name: string, data?: object, androidSystem?: boolean }) => { + publish: (args: { biz?: string, name: string, data?: object, androidSystem?: boolean }) => { if (args.data !== undefined) { (args as any).data = JSON.stringify(args.data) } return context.notification.publish(args) }, - subscribe: (args: { biz: string, name: string, callback: (data?: any) => void, androidSystem?: boolean }) => { + subscribe: (args: { biz?: string, name: string, callback: (data?: any) => void, androidSystem?: boolean }) => { (args as any).callback = (context as any).function2Id(args.callback) return context.notification.subscribe(args) as Promise },