Notifications not working with Android oneplus3 phone

156 views Asked by At

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");

    }
1

There are 1 answers

3
a23sokolov On BEST ANSWER

Try to set NotificationChannel in notificationBuilder

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity().getApplicationContext(), "my_channel_01")
...