13
13
import android .content .pm .ServiceInfo ;
14
14
import android .graphics .Bitmap ;
15
15
import android .graphics .BitmapFactory ;
16
+ import android .graphics .Color ;
17
+ import android .graphics .drawable .Icon ;
16
18
import android .os .Build ;
17
19
import android .os .Bundle ;
18
20
import android .os .Handler ;
@@ -322,11 +324,14 @@ public void onPause() {
322
324
323
325
private void startRecordingService () {
324
326
if (joined ) {
325
- Intent intent = new Intent (requireContext (), LocalRecordingService .class );
326
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
327
- requireContext ().startForegroundService (intent );
328
- } else {
329
- requireContext ().startService (intent );
327
+ Context context = getContext ();
328
+ if (context != null ) {
329
+ Intent intent = new Intent (context , LocalRecordingService .class );
330
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
331
+ context .startForegroundService (intent );
332
+ } else {
333
+ context .startService (intent );
334
+ }
330
335
}
331
336
}
332
337
}
@@ -347,8 +352,11 @@ public void onResume() {
347
352
}
348
353
349
354
private void stopRecordingService () {
350
- Intent intent = new Intent (requireContext (), LocalRecordingService .class );
351
- requireContext ().stopService (intent );
355
+ Context context = getContext ();
356
+ if (context != null ) {
357
+ Intent intent = new Intent (context , LocalRecordingService .class );
358
+ requireContext ().stopService (intent );
359
+ }
352
360
}
353
361
354
362
@ Override
@@ -665,8 +673,8 @@ public void onAudioRouteChanged(int routing) {
665
673
* And the android:foregroundServiceType should be microphone.
666
674
*/
667
675
public static class LocalRecordingService extends Service {
668
- private static final int NOTIFICATION_ID = 1234567800 ;
669
- private static final String CHANNEL_ID = "audio_channel_id " ;
676
+ private static final int NOTIFICATION_ID = 1234567900 ;
677
+ private static final String CHANNEL_ID = "api_audio_channel_id " ;
670
678
671
679
672
680
@ Override
@@ -707,32 +715,38 @@ private Notification getDefaultNotification() {
707
715
icon = R .mipmap .ic_launcher ;
708
716
}
709
717
710
- if (Build .VERSION .SDK_INT >= 26 ) {
718
+ Intent intent = new Intent (this , MainActivity .class );
719
+ intent .setAction ("io.agora.api.example.ACTION_NOTIFICATION_CLICK" );
720
+ intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP | Intent .FLAG_ACTIVITY_CLEAR_TOP );
721
+ int requestCode = (int ) System .currentTimeMillis ();
722
+
723
+ PendingIntent activityPendingIntent = PendingIntent .getActivity (
724
+ this , requestCode , intent , PendingIntent .FLAG_UPDATE_CURRENT | PendingIntent .FLAG_IMMUTABLE
725
+ );
726
+
727
+ Notification .Builder builder ;
728
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
711
729
NotificationChannel mChannel = new NotificationChannel (CHANNEL_ID , name , NotificationManager .IMPORTANCE_DEFAULT );
712
730
NotificationManager mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
713
731
mNotificationManager .createNotificationChannel (mChannel );
714
- }
715
-
716
- PendingIntent activityPendingIntent ;
717
- Intent intent = new Intent ();
718
- intent .setClass (this , MainActivity .class );
719
- if (Build .VERSION .SDK_INT >= 23 ) {
720
- activityPendingIntent = PendingIntent .getActivity (this , 0 , intent , PendingIntent .FLAG_ONE_SHOT | PendingIntent .FLAG_IMMUTABLE );
732
+ builder = new Notification .Builder (this , CHANNEL_ID );
721
733
} else {
722
- activityPendingIntent = PendingIntent . getActivity (this , 0 , intent , PendingIntent . FLAG_ONE_SHOT );
734
+ builder = new Notification . Builder (this );
723
735
}
724
736
725
- Notification .Builder builder = new Notification .Builder (this )
726
- .addAction (icon , "Back to app" , activityPendingIntent )
727
- .setContentText ("Agora Recording ..." )
737
+ builder .setContentTitle ("Agora Recording ..." )
738
+ .setContentText ("Tap here to return to the app." )
739
+ .setContentIntent (activityPendingIntent )
740
+ .setAutoCancel (true )
728
741
.setOngoing (true )
729
742
.setPriority (Notification .PRIORITY_HIGH )
730
743
.setSmallIcon (icon )
731
- .setTicker ( name )
744
+ .setVisibility ( Notification . VISIBILITY_PUBLIC )
732
745
.setWhen (System .currentTimeMillis ());
733
- if (Build .VERSION .SDK_INT >= 26 ) {
734
- builder .setChannelId (CHANNEL_ID );
735
- }
746
+
747
+ Icon iconObj = Icon .createWithResource (this , icon );
748
+ Notification .Action action = new Notification .Action .Builder (iconObj , "Return to the app" , activityPendingIntent ).build ();
749
+ builder .addAction (action );
736
750
737
751
return builder .build ();
738
752
}
0 commit comments