Media player notification created with foreground service not showing on lock screen (Android 13, SDK 34)

  1. Called from HomeActivity

    This is the part where the service function is called from the main activity. It is called differently depending on the version.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>Intent serviceIntent = new Intent(HomeActivity.this, MyService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(serviceIntent);
    } else {
    startService(serviceIntent);
    }
    </code>
    <code>Intent serviceIntent = new Intent(HomeActivity.this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(serviceIntent); } else { startService(serviceIntent); } </code>
    Intent serviceIntent = new Intent(HomeActivity.this, MyService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(serviceIntent);
    } else {
        startService(serviceIntent);
    }
    
  2. MyService Class

    The NotificationManager.IMPORTANCE_HIGH is specified. The setLockscreenVisibility(Notification.VISIBILITY_PUBLIC) is set.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>public class MyService extends Service {
    private static final String CHANNEL_ID = "TTS_NOTI";
    private static final int NOTIFICATION_ID = 999;
    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    createNotificationChannel();
    showTTSNotification(); // Create notification
    return START_STICKY;
    }
    private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(
    CHANNEL_ID,
    "TTS Notifications",
    NotificationManager.IMPORTANCE_HIGH
    );
    channel.setDescription("Notifications for TTS control");
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    NotificationManager manager = getSystemService(NotificationManager.class);
    manager.createNotificationChannel(channel);
    }
    }
    private void showTTSNotification() {
    // MediaSession setup
    MediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession");
    mediaSession.setActive(true);
    // Set large icon
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    // Create intent to bring the app to the foreground
    Intent notificationIntent = new Intent(this, HomeActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    // Create NotificationCompat.Builder
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.icon)
    .setLargeIcon(largeIcon)
    .setContentTitle("Song Title")
    .setContentText("Artist Name")
    .setContentIntent(pendingIntent)
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .setOngoing(true)
    .setCategory(NotificationCompat.CATEGORY_SERVICE)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken())
    .setShowActionsInCompactView(0, 1, 2))
    .addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS")))
    .addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE")))
    .addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT")));
    // Run the service in the foreground
    startForeground(NOTIFICATION_ID, builder.build());
    }
    private PendingIntent getPendingIntent(String action) {
    Intent intent = new Intent(this, NotificationReceiver.class);
    intent.setAction(action);
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }
    @Override
    public void onDestroy() {
    super.onDestroy();
    Log.e("MyService", "Service destroyed");
    }
    }
    </code>
    <code>public class MyService extends Service { private static final String CHANNEL_ID = "TTS_NOTI"; private static final int NOTIFICATION_ID = 999; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); showTTSNotification(); // Create notification return START_STICKY; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "TTS Notifications", NotificationManager.IMPORTANCE_HIGH ); channel.setDescription("Notifications for TTS control"); channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } private void showTTSNotification() { // MediaSession setup MediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession"); mediaSession.setActive(true); // Set large icon Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon); // Create intent to bring the app to the foreground Intent notificationIntent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); // Create NotificationCompat.Builder NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setLargeIcon(largeIcon) .setContentTitle("Song Title") .setContentText("Artist Name") .setContentIntent(pendingIntent) .setPriority(NotificationCompat.PRIORITY_MAX) .setOngoing(true) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken()) .setShowActionsInCompactView(0, 1, 2)) .addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS"))) .addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE"))) .addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT"))); // Run the service in the foreground startForeground(NOTIFICATION_ID, builder.build()); } private PendingIntent getPendingIntent(String action) { Intent intent = new Intent(this, NotificationReceiver.class); intent.setAction(action); return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } @Override public void onDestroy() { super.onDestroy(); Log.e("MyService", "Service destroyed"); } } </code>
    public class MyService extends Service {
        private static final String CHANNEL_ID = "TTS_NOTI";
        private static final int NOTIFICATION_ID = 999;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            createNotificationChannel();
            showTTSNotification();  // Create notification
            return START_STICKY;
        }
    
        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        CHANNEL_ID,
                        "TTS Notifications",
                        NotificationManager.IMPORTANCE_HIGH
                );
                channel.setDescription("Notifications for TTS control");
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(channel);
            }
        }
    
        private void showTTSNotification() {
            // MediaSession setup
            MediaSessionCompat mediaSession = new MediaSessionCompat(this, "TTS_MediaSession");
            mediaSession.setActive(true);
    
            // Set large icon
            Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    
            // Create intent to bring the app to the foreground
            Intent notificationIntent = new Intent(this, HomeActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    
            // Create NotificationCompat.Builder
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon)
                    .setLargeIcon(largeIcon)
                    .setContentTitle("Song Title")
                    .setContentText("Artist Name")
                    .setContentIntent(pendingIntent)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setOngoing(true)
                    .setCategory(NotificationCompat.CATEGORY_SERVICE)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setStyle(new MediaStyle().setMediaSession(mediaSession.getSessionToken())
                            .setShowActionsInCompactView(0, 1, 2))
                    .addAction(new NotificationCompat.Action(R.drawable.play_prev, "Previous", getPendingIntent("PREVIOUS")))
                    .addAction(new NotificationCompat.Action(R.drawable.play, "Play", getPendingIntent("PLAY_PAUSE")))
                    .addAction(new NotificationCompat.Action(R.drawable.play_next, "Next", getPendingIntent("NEXT")));
    
            // Run the service in the foreground
            startForeground(NOTIFICATION_ID, builder.build());
        }
    
        private PendingIntent getPendingIntent(String action) {
            Intent intent = new Intent(this, NotificationReceiver.class);
            intent.setAction(action);
            return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.e("MyService", "Service destroyed");
        }
    }
    
  3. Parts written in AndroiManifest.xml

    And I have added the necessary permissions in the AndroidManifest.

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code><uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
    <serviceandroid:name=".etc.MyService"
    android:foregroundServiceType="mediaPlayback"
    android:permission="android.permission.FOREGROUND_SERVICE"/>
    <receiver android:name=".etc.NotificationReceiver"/>
    </code>
    <code><uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /> <serviceandroid:name=".etc.MyService" android:foregroundServiceType="mediaPlayback" android:permission="android.permission.FOREGROUND_SERVICE"/> <receiver android:name=".etc.NotificationReceiver"/> </code>
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
    
    
    <serviceandroid:name=".etc.MyService"
    android:foregroundServiceType="mediaPlayback"
    android:permission="android.permission.FOREGROUND_SERVICE"/>
    <receiver android:name=".etc.NotificationReceiver"/>
    

I have searched extensively on Google but have not found a solution. I would greatly appreciate any links or information that you could provide.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật