@@ -65,34 +65,70 @@ public void writeResult(PrintWriter out) {
6565 }
6666
6767 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
68- String priorityExtra = intent .getStringExtra ("priority" );
69- if (priorityExtra == null ) priorityExtra = "default" ;
70- int importance ;
71- switch (priorityExtra ) {
72- case "high" :
73- case "max" :
74- importance = NotificationManager .IMPORTANCE_HIGH ;
75- break ;
76- case "low" :
77- importance = NotificationManager .IMPORTANCE_LOW ;
78- break ;
79- case "min" :
80- importance = NotificationManager .IMPORTANCE_MIN ;
81- break ;
82- default :
83- importance = NotificationManager .IMPORTANCE_DEFAULT ;
84- }
8568 NotificationChannel channel = new NotificationChannel (CHANNEL_ID ,
86- CHANNEL_TITLE , importance );
69+ CHANNEL_TITLE , priorityFromIntent ( intent ) );
8770 manager .createNotificationChannel (channel );
88- notification .setChannelId (CHANNEL_ID );
8971 }
9072
9173 manager .notify (notificationId , 0 , notification .build ());
9274 }
9375 });
9476 }
9577
78+ public static void onReceiveChannel (TermuxApiReceiver apiReceiver , final Context context , final Intent intent ) {
79+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
80+ try {
81+ NotificationManager m = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
82+ String channelId = intent .getStringExtra ("id" );
83+ String channelName = intent .getStringExtra ("name" );
84+
85+ if (channelId == null || channelId .equals ("" )) {
86+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Channel id not specified." ));
87+ return ;
88+ }
89+
90+ if (intent .getBooleanExtra ("delete" ,false )) {
91+ m .deleteNotificationChannel (channelId );
92+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Deleted channel with id \" " +channelId +"\" ." ));
93+ return ;
94+ }
95+
96+ if (channelName == null || channelName .equals ("" )) {
97+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Cannot create a channel without a name." ));
98+ }
99+
100+ NotificationChannel c = new NotificationChannel (channelId , channelName , priorityFromIntent (intent ));
101+ m .createNotificationChannel (c );
102+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Created channel with id \" " +channelId +"\" and name \" " +channelName +"\" ." ));
103+ } catch (Exception e ) {
104+ e .printStackTrace ();
105+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Could not create/delete channel." ));
106+ }
107+ } else {
108+ ResultReturner .returnData (apiReceiver , intent , out -> out .println ("Notification channels are only available on Android 8.0 and higher, use the options for termux-notification instead." ));
109+ }
110+ }
111+
112+ private static int priorityFromIntent (Intent intent ) {
113+ String priorityExtra = intent .getStringExtra ("priority" );
114+ if (priorityExtra == null ) priorityExtra = "default" ;
115+ int importance ;
116+ switch (priorityExtra ) {
117+ case "high" :
118+ case "max" :
119+ importance = NotificationManager .IMPORTANCE_HIGH ;
120+ break ;
121+ case "low" :
122+ importance = NotificationManager .IMPORTANCE_LOW ;
123+ break ;
124+ case "min" :
125+ importance = NotificationManager .IMPORTANCE_MIN ;
126+ break ;
127+ default :
128+ importance = NotificationManager .IMPORTANCE_DEFAULT ;
129+ }
130+ return importance ;
131+ }
96132
97133 static Pair <NotificationCompat .Builder , String > buildNotification (final Context context , final Intent intent ) {
98134 String priorityExtra = intent .getStringExtra ("priority" );
@@ -142,9 +178,14 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context
142178 final String notificationId = getNotificationId (intent );
143179
144180 String groupKey = intent .getStringExtra ("group" );
145-
181+
182+ String channel = intent .getStringExtra ("channel" );
183+ if (channel == null ) {
184+ channel = CHANNEL_ID ;
185+ }
186+
146187 final NotificationCompat .Builder notification = new NotificationCompat .Builder (context ,
147- CHANNEL_ID );
188+ channel );
148189 notification .setSmallIcon (R .drawable .ic_event_note_black_24dp );
149190 notification .setColor (0xFF000000 );
150191 notification .setContentTitle (title );
0 commit comments