3

I have this function for notification generation.I have updated the code as per oreo requirements as well. What could be the issue here. Any help is appreciated.It is working on all the other phones.I have rechecked the settings for notifications as well

private void sendNotification(String messageTitle,String messageBody) {
        android.net.Uri sound;
        Intent intent = new Intent(getActivity(), MainActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0 /* request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        long[] pattern = {500, 500, 500, 500, 500};
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        SharedPreferences demoprefs = getActivity().getSharedPreferences("demo", MODE_PRIVATE);
        demiIsLoggedIn = demoprefs.getBoolean("demo", true);
        if (!demiIsLoggedIn)
            sound = defaultSoundUri;
        else
            sound = null;
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getActivity())
                .setSmallIcon(getActivity().getApplicationInfo().icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setLights(Color.BLUE, 1, 1)
                .setSound(sound)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String CHANNEL_ID = "my_channel_01";// The id of the channel.
            CharSequence name = "notification channel";// The user-visible name of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(12 /* ID of notification */, notificationBuilder.build());
        Log.d("wajdan","oneplus");

    }
Wajdan Ali
  • 159
  • 12

1 Answers1

1

Try to set NotificationChannel in notificationBuilder

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity().getApplicationContext(), "my_channel_01")
...
a23sokolov
  • 510
  • 2
  • 9
  • I believe the 2-arg constructor is the new thing for NotificationCompat.Builder as discussed here https://stackoverflow.com/questions/45462666/notificationcompat-builder-deprecated-in-android-o – Wajdan Ali Mar 22 '18 at 11:23
  • but It shows an error saying " Builder (android.content.context) Builder cannot be applied to (class,java.lang.string)" , I am using the build tools 27.0.3, what to change now? – Wajdan Ali Mar 22 '18 at 11:23
  • I edit answer try with getActivity().getApplicationContext() – a23sokolov Mar 22 '18 at 13:45