2020import com .facebook .react .bridge .ReactContextBaseJavaModule ;
2121import com .facebook .react .bridge .ReactMethod ;
2222import com .facebook .react .bridge .ReadableMap ;
23+ import com .facebook .react .bridge .ReadableMapKeySetIterator ;
2324import com .facebook .react .bridge .WritableMap ;
2425import com .facebook .react .modules .core .RCTNativeAppEventEmitter ;
2526
2627import java .lang .reflect .Array ;
28+ import java .util .ArrayList ;
2729import java .util .Arrays ;
2830import java .util .Collections ;
2931import java .util .List ;
@@ -109,31 +111,6 @@ public static void sendEvent(String key, WritableMap event) {
109111 }
110112 }
111113
112- private void createNotificationChannel () {
113- // Create the NotificationChannel, but only on API 26+ because
114- // the NotificationChannel class is new and not in the support library
115- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
116- // String description = getString(R.string.channel_description);
117- int importance = NotificationManager .IMPORTANCE_DEFAULT ;
118- NotificationChannelGroup customerGroup = new NotificationChannelGroup ("TODO" , "企业给员工的任务" );
119- NotificationChannelGroup todoGroup = new NotificationChannelGroup ("Customer Dynamics" , "客户动态" );
120-
121- NotificationChannel customerChannel = new NotificationChannel ("Customer Dynamics" , "客户动态通知" , importance );
122- customerChannel .setGroup (customerGroup .getId ());
123- NotificationChannel hignChannel = new NotificationChannel ("high_system" , "服务提醒" , importance );
124- hignChannel .setGroup (customerGroup .getId ());
125- NotificationChannel todoChannel = new NotificationChannel ("TODO" , "企业给员工的任务" , importance );
126- todoChannel .setGroup (todoGroup .getId ());
127- // channel.setDescription(description);
128- // Register the channel with the system; you can't change the importance
129- // or other notification behaviors after this
130- NotificationManager notificationManager = __rac .getSystemService (NotificationManager .class );
131- notificationManager .createNotificationChannelGroups (Arrays .asList (customerGroup , todoGroup ));
132- notificationManager .createNotificationChannels (Arrays .asList (customerChannel , hignChannel ,todoChannel ));
133-
134- }
135- }
136-
137114 @ ReactMethod
138115 public void init (ReadableMap configs , Promise promise ) {
139116 ReadableMap config = null ;
@@ -148,7 +125,7 @@ public void init(ReadableMap configs, Promise promise) {
148125 Logger .i ("init Success!" );
149126 promise .resolve (null );
150127
151- createNotificationChannel ();
128+ // createNotificationChannel();
152129
153130 } catch (Throwable e ) {
154131 e .printStackTrace ();
@@ -294,6 +271,66 @@ public void checkPermission(Promise promise) {
294271 promise .resolve (state );
295272 }
296273
274+ /**
275+ * config:{
276+ * [groupId]:{
277+ * name,
278+ * channelDic:{
279+ * [channelId]:{
280+ * name,
281+ * importance,
282+ * }
283+ * }
284+ * }
285+ * }
286+ * @param config
287+ * @param promise
288+ */
289+ @ ReactMethod
290+ public void createNotificationChannels (ReadableMap config , Promise promise ){
291+ try {
292+ if (config == null ){
293+ promise .resolve (null );
294+ return ;
295+ }
296+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
297+ // String description = getString(R.string.channel_description);
298+ // int importance = NotificationManager.IMPORTANCE_DEFAULT;
299+ ReadableMapKeySetIterator iterator = config .keySetIterator ();
300+ ArrayList groups = new ArrayList <NotificationChannelGroup >();
301+ ArrayList channels = new ArrayList <NotificationChannel >();
302+ while (iterator .hasNextKey ()){
303+ String groupId = iterator .nextKey ();
304+ ReadableMap groupConfig = config .getMap (groupId );
305+ String groupName = groupConfig .getString ("name" );
306+ ReadableMap channelDic = groupConfig .getMap ("channelDic" );
307+ NotificationChannelGroup group = new NotificationChannelGroup (groupId ,groupName );
308+ ReadableMapKeySetIterator cIt = channelDic .keySetIterator ();
309+ groups .add (group );
310+ while (cIt .hasNextKey ()){
311+ String channelId = cIt .nextKey ();
312+ ReadableMap channelConfig = channelDic .getMap (channelId );
313+ String channelName = channelConfig .getString ("name" );
314+ int importance = channelConfig .getInt ("importance" );
315+ NotificationChannel channel = new NotificationChannel (channelId ,channelName ,importance );
316+ channel .setGroup (group .getId ());
317+ channels .add (channel );
318+ }
319+ }
320+
321+ // Register the channel with the system; you can't change the importance
322+ // or other notification behaviors after this
323+ NotificationManager notificationManager = __rac .getSystemService (NotificationManager .class );
324+ notificationManager .createNotificationChannelGroups (groups );
325+ notificationManager .createNotificationChannels (channels );
326+ }
327+ promise .resolve (null );
328+ }catch (Throwable e ){
329+ e .printStackTrace ();
330+ promise .reject (e );
331+ }
332+ }
333+
297334 @ ReactMethod
298335 public void checkNetwork (Promise promise ) {
299336 try {
0 commit comments