1

I am developing an app and I want to play background music, so I use service for playing and stopping. It works perfectly. Also, I want to show notification for pausing, playing..., but the notification doesn't appear on Android 8.0. I use andorid 3.0.1 , compileSdkVersion=26 , targetSdkVersion=26 , minSdkVersion=15

this is my current code

public class BackgroundSoundService extends Service {
    private static final String TAG = "BackgroundSoundService";
    MediaPlayer player;
    int length;
    static int t;
    private static final String LOG_TAG = "ForegroundService";
    String channelId = "channel-01";
    Notification notification;
    NotificationChannel mChannel;
    String name = "volume";
    int id = 0;
    int importance = NotificationManager.IMPORTANCE_HIGH;

    public IBinder onBind(Intent arg0) {
        Log.i(TAG, "onBind()");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.jorgesys_song);
       ....
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);

            Intent previousIntent = new Intent(this, BackgroundSoundService.class);
            previousIntent.setAction(Constants.ACTION.PREV_ACTION);
            PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
                    previousIntent, 0);

            Intent playIntent = new Intent(this, BackgroundSoundService.class);
            playIntent.setAction(Constants.ACTION.PLAY_ACTION);
            PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                    playIntent, 0);

            Intent nextIntent = new Intent(this, BackgroundSoundService.class);
            nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
            PendingIntent pnextIntent = PendingIntent.getService(this, 0,
                    nextIntent, 0);

            Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.esp);

            notification = new NotificationCompat.Builder(this)
                    .setContentTitle("Truiton Music Player")
                    .setTicker("Truiton Music Player")
                    .setContentText("My Music")
                    .setSmallIcon(R.drawable.esp)
                    .setLargeIcon(
                            Bitmap.createScaledBitmap(icon, 128, 128, false))
                    .setContentIntent(pendingIntent)
                    .setOngoing(true)
                    .setPriority(Notification.PRIORITY_MAX).setWhen(0)
                    .addAction(R.drawable.ic_per,
                            "", ppreviousIntent)
                    .addAction(R.drawable.ic_play, "",
                            pplayIntent)
                    .addAction(R.drawable.ic_next, "",
                            pnextIntent).build();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(channelId, name,
                        importance);
                mChannel.setShowBadge(true);
                NotificationManager mNotifyMgr =
                        (NotificationManager)
                                getSystemService(Context.NOTIFICATION_SERVICE);
                mNotifyMgr.createNotificationChannel(mChannel);

                // Builds the notification and issues it.
                mNotifyMgr.notify(id, notification);
            }


            startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                    notification);


        } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
            Log.i(LOG_TAG, "Clicked Previous");
        } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
            Log.i(LOG_TAG, "Clicked Play");
        } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
            Log.i(LOG_TAG, "Clicked Next");
        } else if (intent.getAction().equals(
                Constants.ACTION.STOPFOREGROUND_ACTION)) {
            Log.i(LOG_TAG, "Received Stop Foreground Intent");
            stopForeground(true);
            stopSelf();
        }


        player.start();
        return Service.START_STICKY;
    }

and I have another question: in a notification, I have three buttons and I add 3 images for them, but their background doesn't change. Thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Monir
  • 41
  • 8
  • I've read about my problem but I can't add setChannelId(CHANNEL_ID), it can't resolve. – Monir Sep 02 '18 at 07:29
  • You should do that – Radesh Sep 02 '18 at 07:33
  • NotificationCompatBuilder.setChannelId() (https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder#setchannelid) requires version 26.1.0 or higher of the compat library. Check your build file and make sure your compat libraries are at least that version. – Ridcully Sep 02 '18 at 09:28

2 Answers2

0

add this line in if condition

notification.setChannelId(mChannel);

Like this :

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(channelId, name,
                    importance);
            mChannel.setShowBadge(true);

            notification.setChannelId(mChannel);//add this line

            NotificationManager mNotifyMgr =
                    (NotificationManager)
                            getSystemService(Context.NOTIFICATION_SERVICE);
            mNotifyMgr.createNotificationChannel(mChannel);

            // Builds the notification and issues it.
            mNotifyMgr.notify(id, notification);
        }
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

Please try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);
        mChannel.setShowBadge(true);

        NotificationManager mNotifyMgr =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifyMgr.createNotificationChannel(mChannel);

        notification = new Notification.Builder(this)
                .setChannelId(CHANNEL_ID)  // CHANNEL_ID = "notification"
                .setContentTitle("Truiton Music Player")
                .setTicker("Truiton Music Player")
                .setContentText("My Music")
                .setSmallIcon(R.drawable.esp)
                .setLargeIcon(
                        Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_MAX).setWhen(0)
                .addAction(R.drawable.ic_per,
                        "", ppreviousIntent)
                .addAction(R.drawable.ic_play, "",
                        pplayIntent)
                .addAction(R.drawable.ic_next, "",
                        pnextIntent).build();


        // Builds the notification and issues it.
        mNotifyMgr.notify(id, notification);
    }

Also please add RECEIVE_SMS permission on runtime.

Avilash
  • 176
  • 1
  • 9