1313import android .content .pm .ServiceInfo ;
1414import android .graphics .Bitmap ;
1515import android .graphics .BitmapFactory ;
16+ import android .graphics .Color ;
17+ import android .graphics .drawable .Icon ;
1618import android .os .Build ;
1719import android .os .Bundle ;
1820import android .os .Handler ;
@@ -322,11 +324,14 @@ public void onPause() {
322324
323325 private void startRecordingService () {
324326 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+ }
330335 }
331336 }
332337 }
@@ -347,8 +352,11 @@ public void onResume() {
347352 }
348353
349354 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+ }
352360 }
353361
354362 @ Override
@@ -665,8 +673,8 @@ public void onAudioRouteChanged(int routing) {
665673 * And the android:foregroundServiceType should be microphone.
666674 */
667675 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 " ;
670678
671679
672680 @ Override
@@ -707,32 +715,38 @@ private Notification getDefaultNotification() {
707715 icon = R .mipmap .ic_launcher ;
708716 }
709717
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 ) {
711729 NotificationChannel mChannel = new NotificationChannel (CHANNEL_ID , name , NotificationManager .IMPORTANCE_DEFAULT );
712730 NotificationManager mNotificationManager = (NotificationManager ) this .getSystemService (Context .NOTIFICATION_SERVICE );
713731 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 );
721733 } else {
722- activityPendingIntent = PendingIntent . getActivity (this , 0 , intent , PendingIntent . FLAG_ONE_SHOT );
734+ builder = new Notification . Builder (this );
723735 }
724736
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 )
728741 .setOngoing (true )
729742 .setPriority (Notification .PRIORITY_HIGH )
730743 .setSmallIcon (icon )
731- .setTicker ( name )
744+ .setVisibility ( Notification . VISIBILITY_PUBLIC )
732745 .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 );
736750
737751 return builder .build ();
738752 }
0 commit comments