|
15 | 15 | */
|
16 | 16 | package androidx.media3.demo.session
|
17 | 17 |
|
18 |
| -import android.app.Notification.BigTextStyle |
19 | 18 | import android.app.NotificationChannel
|
20 | 19 | import android.app.NotificationManager
|
21 | 20 | import android.app.PendingIntent.*
|
@@ -272,34 +271,29 @@ class PlaybackService : MediaLibraryService() {
|
272 | 271 | * background.
|
273 | 272 | */
|
274 | 273 | override fun onForegroundServiceStartNotAllowedException() {
|
275 |
| - createNotificationAndNotify() |
| 274 | + val notificationManagerCompat = NotificationManagerCompat.from(this@PlaybackService) |
| 275 | + ensureNotificationChannel(notificationManagerCompat) |
| 276 | + val pendingIntent = |
| 277 | + TaskStackBuilder.create(this@PlaybackService).run { |
| 278 | + addNextIntent(Intent(this@PlaybackService, MainActivity::class.java)) |
| 279 | + |
| 280 | + val immutableFlag = if (Build.VERSION.SDK_INT >= 23) FLAG_IMMUTABLE else 0 |
| 281 | + getPendingIntent(0, immutableFlag or FLAG_UPDATE_CURRENT) |
| 282 | + } |
| 283 | + val builder = |
| 284 | + NotificationCompat.Builder(this@PlaybackService, CHANNEL_ID) |
| 285 | + .setContentIntent(pendingIntent) |
| 286 | + .setSmallIcon(R.drawable.media3_notification_small_icon) |
| 287 | + .setContentTitle(getString(R.string.notification_content_title)) |
| 288 | + .setStyle( |
| 289 | + NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_content_text)) |
| 290 | + ) |
| 291 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT) |
| 292 | + .setAutoCancel(true) |
| 293 | + notificationManagerCompat.notify(NOTIFICATION_ID, builder.build()) |
276 | 294 | }
|
277 | 295 | }
|
278 | 296 |
|
279 |
| - private fun createNotificationAndNotify() { |
280 |
| - var notificationManagerCompat = NotificationManagerCompat.from(this) |
281 |
| - ensureNotificationChannel(notificationManagerCompat) |
282 |
| - var pendingIntent = |
283 |
| - TaskStackBuilder.create(this).run { |
284 |
| - addNextIntent(Intent(this@PlaybackService, MainActivity::class.java)) |
285 |
| - |
286 |
| - val immutableFlag = if (Build.VERSION.SDK_INT >= 23) FLAG_IMMUTABLE else 0 |
287 |
| - getPendingIntent(0, immutableFlag or FLAG_UPDATE_CURRENT) |
288 |
| - } |
289 |
| - |
290 |
| - var builder = |
291 |
| - NotificationCompat.Builder(this, CHANNEL_ID) |
292 |
| - .setContentIntent(pendingIntent) |
293 |
| - .setSmallIcon(R.drawable.media3_notification_small_icon) |
294 |
| - .setContentTitle(getString(R.string.notification_content_title)) |
295 |
| - .setStyle( |
296 |
| - NotificationCompat.BigTextStyle().bigText(getString(R.string.notification_content_text)) |
297 |
| - ) |
298 |
| - .setPriority(NotificationCompat.PRIORITY_DEFAULT) |
299 |
| - .setAutoCancel(true) |
300 |
| - notificationManagerCompat.notify(NOTIFICATION_ID, builder.build()) |
301 |
| - } |
302 |
| - |
303 | 297 | private fun ensureNotificationChannel(notificationManagerCompat: NotificationManagerCompat) {
|
304 | 298 | if (Util.SDK_INT < 26 || notificationManagerCompat.getNotificationChannel(CHANNEL_ID) != null) {
|
305 | 299 | return
|
|
0 commit comments