8

How to enable heads-up notifications programatically on Xiaomi devices? Some apps (for example Telegram) have this permisstions turned on by default.

Settings screenshot: https://i.stack.imgur.com/kSsyD.jpg

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel( ) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    channel.enableLights(true);
    channel.setLightColor(Color.RED);
    channel.enableVibration(true);
    channel.setSound(createNotificationSound(),
            new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build());
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);

    return CHANNEL_ID;
}

Notification

    String channelId = "";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelId = createNotificationChannel();
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
            .setLargeIcon(icon)
            .setSmallIcon(R.mipmap.ic_mipmap_launcher)
            .setContentTitle("Title")
            .setTicker(getResources().getString(R.string.app_name))
            .setContentText("Text")
            .setSound(createNotificationSound(), AudioManager.STREAM_NOTIFICATION)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setChannelId(channelId)
            .setPriority(Notification.PRIORITY_HIGH);

    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    notificationBuilder.setStyle(inboxStyle);
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = notificationBuilder.build();
    notificationManager.notify(order.getId(), notification);`
Yevhenii
  • 810
  • 9
  • 12
  • It should work by using NotificationManager.IMPORTANCE_HIGH as other mobiles. I don't know why doesn't it work in Xiaomi! I have the same problem – Momen Zaqout May 19 '19 at 23:11
  • 1
    There are reports out there that I could not verify but they basically claim that Chinese phone manufacturers "whilelist" popular apps like WhatsApp, Telegram, etc while they restrictively block all other apps when it comes to certain permissions. The Telegram Android app is open sourced on Github and they don't even seem to use `ACCESS_NOTIFICATION_POLICY` as many suggest on SO regarding the issue you describe. – Manuel Dec 19 '19 at 11:06

0 Answers0