Android Notification add permission

This commit is contained in:
pengfei.zhou 2020-12-08 12:30:35 +08:00 committed by osborn
parent a355361f33
commit ac2a4121c2
4 changed files with 20 additions and 6 deletions

View File

@ -46,8 +46,8 @@ import pub.doric.extension.bridge.DoricPromise;
@DoricPlugin(name = "notification") @DoricPlugin(name = "notification")
public class NotificationPlugin extends DoricJavaPlugin { public class NotificationPlugin extends DoricJavaPlugin {
private HashMap<String, BroadcastReceiver> systemReceivers = new HashMap<>(); private final HashMap<String, BroadcastReceiver> systemReceivers = new HashMap<>();
private HashMap<String, BroadcastReceiver> localReceivers = new HashMap<>(); private final HashMap<String, BroadcastReceiver> localReceivers = new HashMap<>();
public NotificationPlugin(DoricContext doricContext) { public NotificationPlugin(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -74,7 +74,12 @@ public class NotificationPlugin extends DoricJavaPlugin {
Intent intent = new Intent(name); Intent intent = new Intent(name);
intent.putExtra("__doric_data__", data); intent.putExtra("__doric_data__", data);
if (androidSystem) { if (androidSystem) {
getDoricContext().getContext().sendBroadcast(intent); String permission = null;
JSValue jsValue = args.getProperty("permission");
if (jsValue.isString()) {
permission = jsValue.asString().value();
}
getDoricContext().getContext().sendBroadcast(intent, permission);
} else { } else {
LocalBroadcastManager.getInstance(getDoricContext().getContext()).sendBroadcast(intent); LocalBroadcastManager.getInstance(getDoricContext().getContext()).sendBroadcast(intent);
} }
@ -125,7 +130,12 @@ public class NotificationPlugin extends DoricJavaPlugin {
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(name); intentFilter.addAction(name);
if (androidSystem) { if (androidSystem) {
getDoricContext().getContext().registerReceiver(receiver, intentFilter); String permission = null;
JSValue jsValue = args.getProperty("permission");
if (jsValue.isString()) {
permission = jsValue.asString().value();
}
getDoricContext().getContext().registerReceiver(receiver, intentFilter, permission, null);
systemReceivers.put(callbackId, receiver); systemReceivers.put(callbackId, receiver);
} else { } else {
LocalBroadcastManager.getInstance(getDoricContext().getContext()) LocalBroadcastManager.getInstance(getDoricContext().getContext())

2
doric-js/index.d.ts vendored
View File

@ -926,12 +926,14 @@ declare module 'doric/lib/src/native/notification' {
name: string; name: string;
data?: object; data?: object;
androidSystem?: boolean; androidSystem?: boolean;
permission?: string;
}) => Promise<any>; }) => Promise<any>;
subscribe: (args: { subscribe: (args: {
biz?: string | undefined; biz?: string | undefined;
name: string; name: string;
callback: (data?: any) => void; callback: (data?: any) => void;
androidSystem?: boolean | undefined; androidSystem?: boolean | undefined;
permission?: string | undefined;
}) => Promise<string>; }) => Promise<string>;
unsubscribe: (subscribeId: string) => Promise<any>; unsubscribe: (subscribeId: string) => Promise<any>;
}; };

View File

@ -5,12 +5,14 @@ export declare function notification(context: BridgeContext): {
name: string; name: string;
data?: object; data?: object;
androidSystem?: boolean; androidSystem?: boolean;
permission?: string;
}) => Promise<any>; }) => Promise<any>;
subscribe: (args: { subscribe: (args: {
biz?: string | undefined; biz?: string | undefined;
name: string; name: string;
callback: (data?: any) => void; callback: (data?: any) => void;
androidSystem?: boolean | undefined; androidSystem?: boolean | undefined;
permission?: string | undefined;
}) => Promise<string>; }) => Promise<string>;
unsubscribe: (subscribeId: string) => Promise<any>; unsubscribe: (subscribeId: string) => Promise<any>;
}; };

View File

@ -16,13 +16,13 @@
import { BridgeContext } from "../runtime/global" import { BridgeContext } from "../runtime/global"
export function notification(context: BridgeContext) { export function notification(context: BridgeContext) {
return { return {
publish: (args: { biz?: string, name: string, data?: object, androidSystem?: boolean }) => { publish: (args: { biz?: string, name: string, data?: object, androidSystem?: boolean, permission?: string }) => {
if (args.data !== undefined) { if (args.data !== undefined) {
(args as any).data = JSON.stringify(args.data) (args as any).data = JSON.stringify(args.data)
} }
return context.callNative('notification', 'publish', args) return context.callNative('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, permission?: string }) => {
(args as any).callback = context.function2Id(args.callback) (args as any).callback = context.function2Id(args.callback)
return context.callNative('notification', 'subscribe', args) as Promise<string> return context.callNative('notification', 'subscribe', args) as Promise<string>
}, },