@@ -22,7 +22,7 @@ sealed partial class MediaControlsService : Service
2222 bool isDisposed ;
2323
2424 PlayerNotificationManager ? playerNotificationManager ;
25- NotificationCompat . Builder ? notification ;
25+ NotificationCompat . Builder ? notificationBuilder ;
2626
2727 public event EventHandler TaskRemoved
2828 {
@@ -77,10 +77,10 @@ public override void OnRebind(Intent? intent)
7777 StartForegroundServices ( ) ;
7878 }
7979
80- [ MemberNotNull ( nameof ( NotificationManager ) , nameof ( notification ) ) ]
80+ [ MemberNotNull ( nameof ( NotificationManager ) , nameof ( notificationBuilder ) ) ]
8181 public void UpdateNotifications ( in MediaSession session , in PlatformMediaElement mediaElement )
8282 {
83- ArgumentNullException . ThrowIfNull ( notification ) ;
83+ ArgumentNullException . ThrowIfNull ( notificationBuilder ) ;
8484 ArgumentNullException . ThrowIfNull ( NotificationManager ) ;
8585
8686 var style = new MediaStyleNotificationHelper . MediaStyle ( session ) ;
@@ -89,8 +89,8 @@ public void UpdateNotifications(in MediaSession session, in PlatformMediaElement
8989 SetLegacyNotifications ( session , mediaElement ) ;
9090 }
9191
92- notification . SetStyle ( style ) ;
93- NotificationManagerCompat . From ( Platform . AppContext ) . Notify ( 1 , notification . Build ( ) ) ;
92+ notificationBuilder . SetStyle ( style ) ;
93+ NotificationManagerCompat . From ( Platform . AppContext ) ? . Notify ( 1 , notificationBuilder . Build ( ) ) ;
9494 }
9595
9696 [ MemberNotNull ( nameof ( playerNotificationManager ) ) ]
@@ -152,26 +152,29 @@ static void CreateNotificationChannel(in NotificationManager notificationMnaMana
152152 notificationMnaManager . CreateNotificationChannel ( channel ) ;
153153 }
154154
155- [ MemberNotNull ( nameof ( notification ) , nameof ( NotificationManager ) ) ]
155+ [ MemberNotNull ( nameof ( notificationBuilder ) , nameof ( NotificationManager ) ) ]
156156 void StartForegroundServices ( )
157157 {
158158 NotificationManager ??= GetSystemService ( NotificationService ) as NotificationManager ?? throw new InvalidOperationException ( $ "{ nameof ( NotificationManager ) } cannot be null") ;
159- notification ??= new NotificationCompat . Builder ( Platform . AppContext , "1" ) ;
159+ notificationBuilder ??= new NotificationCompat . Builder ( Platform . AppContext , "1" ) ;
160160
161- notification . SetSmallIcon ( Resource . Drawable . media3_notification_small_icon ) ;
162- notification . SetAutoCancel ( false ) ;
163- notification . SetForegroundServiceBehavior ( NotificationCompat . ForegroundServiceImmediate ) ;
164- notification . SetVisibility ( NotificationCompat . VisibilityPublic ) ;
161+ notificationBuilder . SetSmallIcon ( Resource . Drawable . media3_notification_small_icon ) ;
162+ notificationBuilder . SetAutoCancel ( false ) ;
163+ notificationBuilder . SetForegroundServiceBehavior ( NotificationCompat . ForegroundServiceImmediate ) ;
164+ notificationBuilder . SetVisibility ( NotificationCompat . VisibilityPublic ) ;
165165
166166 CreateNotificationChannel ( NotificationManager ) ;
167167
168168 if ( OperatingSystem . IsAndroidVersionAtLeast ( 29 ) )
169169 {
170- StartForeground ( 1 , notification . Build ( ) , ForegroundService . TypeMediaPlayback ) ;
170+ if ( notificationBuilder . Build ( ) is Notification notification )
171+ {
172+ StartForeground ( 1 , notification , ForegroundService . TypeMediaPlayback ) ;
173+ }
171174 }
172175 else
173176 {
174- StartForeground ( 1 , notification . Build ( ) ) ;
177+ StartForeground ( 1 , notificationBuilder . Build ( ) ) ;
175178 }
176179 }
177180}
0 commit comments