1

I have an app which was built in api level 22, now I had to update it to api level 26 and the persistant notification that was built initially is no longer working. I have tried multiple codes from stackoverflow but with no success for me.

My code:

    void setUpAsForeground(String text) {
        Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent openMainActivity = PendingIntent.getActivity(getApplicationContext(), 0,
                mainActivity, 0);
        // Build the notification object.
        mNotificationBuilder = new Notification.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.radio_icon_128px)
                .setTicker(text)
                .setWhen(0)
//                .setWhen(System.currentTimeMillis())
                .setContentTitle(getResources().getString(R.string.app_name) + text)
                .setContentText(mMusicProvider.getCurrentSongTitle())
                .setContentIntent(openMainActivity)
                .setOngoing(true);
        startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
//        broadcastAction(MainActivity.ACTION_UPDATE_TITLE);
    }

any advice would be highly appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
John Doe
  • 11
  • 1

2 Answers2

1

Starting with API 26, you must provide a notification channel for your app before any notifications will be shown. For Android 7 and lower, setting the priority is required. See this doc for more info and examples: https://developer.android.com/training/notify-user/build-notification

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
0

Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel If all you need is to make a Notification from your app then you consider using this code.

 public class makeNotification {
        private static final String ChannelId = "ChannelId";
        private static final String CHANNEL_ID = "cahhnel";
        private static final int NOTIFICATION_ID = 99;

        public static void makenotification(Context context) {
          Intent intent = new Intent(context,DemoVolleyActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        CHANNEL_ID,
                        "Channel Name",
                        NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
            }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
                    .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                    .setSmallIcon(R.drawable.ic_lock_black_24dp)
                    .setContentTitle("My Notification")
                    .setContentText("notification_body")
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                    && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
            }
            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
        }
    }
Charlyge
  • 1
  • 1