2

I am creating a messaging app so notification play a important role in my app. I have created notification Display handler , notification are working fine on one-plus, Motorola, Samsung but not on Mi devices . By default lock screen notifications are disable for my app in MI devices(MIUI10) . But when I checked setting for popular apps like WhatsApp , Snapchat all the options are enabled like lock screen notification, sound, floating notification.

Manually I can enable all the settings but I want to do it programmatically so that user doesn't need to do it. I tried with notification channel (for above O devices) by using their method setLockscreenVisibility() but it didn't work.

notificationChannel = new NotificationChannel(ChatSDKMessageChannel, name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri, audioAttributes);
channel.enableVibration(true);

Code to display Notification:


 public void createAlertNotification(Context context, Intent resultIntent, String title, String message, Bitmap largeIcon, int smallIconResID, Uri soundUri, int number){

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Notification.Builder builder =
                new Notification.Builder(context)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(smallIconResID)
                        .setVibrate(new long[]{0, 250, 100, 250})
                        .setSound(soundUri)
                        .setNumber(number)
                        .setContentIntent(pendingIntent)
                        .setTicker(title + ": " + message)
                        .setPriority(Notification.PRIORITY_HIGH);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (largeIcon != null) {
            builder.setLargeIcon(ImageUtils.scaleImage(largeIcon, (int) (context.getResources().getDisplayMetrics().density * 48)));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(ChatSDK.config().pushNotificationColor);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            builder.setChannelId(ChatSDKMessageChannel);

            CharSequence name = context.getString(R.string.app_name);
            String description = context.getString(R.string.push_channel_name);

            NotificationChannel channel = new NotificationChannel(ChatSDKMessageChannel, name, NotificationManager.IMPORTANCE_HIGH);
            channel.enableVibration(true);
            channel.setDescription(description);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }

        }


        Notification notification = builder.build();

        notification.flags = Notification.FLAG_AUTO_CANCEL ;

        notificationManager.notify(MESSAGE_NOTIFICATION_ID, notification);

        wakeScreen(context);
    }

screenshot of notification setting of my demo app:

enter image description here

screenshot of WhatsApp:

enter image description here

I need all the notifications options enabled for my app just like WhatsApp and snapchat.

Amal K
  • 4,359
  • 2
  • 22
  • 44
Himanshi Thakur
  • 2,077
  • 14
  • 32

0 Answers0