1

I am working on heads up notification. My code is working fine for all devices but not with xiaomi and LeEco like devices which are above android 5.1. My code is:

RemoteViews contentView = null;
    contentView = new RemoteViews(context.getPackageName(), R.layout.demo);

final android.support.v4.app.NotificationCompat.Builder notification =  new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_ALL) 
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCustomHeadsUpContentView(contentView)
            .setVibrate(new long[0])
            .setCategory(Notification.CATEGORY_CALL)
            .setDeleteIntent(createOnDismissedIntent(context, 2222))
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1111, notification.build());

What could be the issue?

Kavita Patil
  • 1,784
  • 1
  • 17
  • 30
  • Have you get any error then please post here. – Haresh Chhelana Sep 20 '17 at 07:54
  • I don't get any error but not getting heads-up notification, it comes as a normal notification in panel. I checked more , these devices has more permissions for notification. They have made it more restrictive. I need to manually turn those checks on and then i receive heads-up notification. Like LeEco has "banner notification check". – Kavita Patil Sep 20 '17 at 18:32

1 Answers1

3

I think this solution will be very helpfull for you, but I can't find any solution for LeEco Devices:

public class WhiteListUtils {

private static final String SKIP_WHITELIST_APP = "SKIP_WHITELIST_APP";

private static final String XIAOMI = "xiaomi";
private static final String HUAWEI = "huawei";
private static final String OPPO = "oppo";
private static final String VIVO = "vivo";

private static SharedPreferences settings;

private WhiteListUtils() {
}

public static void showWhiteListingApps(Context context) {
    if (settings == null)
        settings = context.getSharedPreferences("WhiteListUtils", MODE_PRIVATE);
    if (!settings.getBoolean(SKIP_WHITELIST_APP, false))
        checkOSCompat(context);
}

private static void checkOSCompat(Context context) {
    try {
        Intent intent = new Intent();
        String manufacturer = android.os.Build.MANUFACTURER;
        if (XIAOMI.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
        } else if (OPPO.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
            intent.setComponent(new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity"));
        } else if (VIVO.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
        } else if (HUAWEI.equalsIgnoreCase(manufacturer)) {
            intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
        }

        if (isCallable(context, intent)) {
            showAlertWindow(context, intent);
        } else {
            if (BuildConfig.BUILD_TYPE.contains("release"))
                Crashlytics.log("Intent not callable for whitelisting " + intent.toString());
            Log.e("Intent not callable for whitelisting " + intent.toString());
        }
    } catch (Exception e) {
        if (BuildConfig.BUILD_TYPE.contains("release")) {
            Crashlytics.logException(e);
        }
        Log.e("checkOSCompat Error " + e.getMessage());
    }
}

private static void showAlertWindow(final Context context, final Intent intent) {
    new AlertDialog.Builder(context)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Protected Apps")
            .setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name)))
            .setPositiveButton("Go to Apps", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    context.startActivity(intent);
                    settings.edit().putBoolean(SKIP_WHITELIST_APP, true).apply();
                }
            })
            .setNegativeButton(android.R.string.cancel, null)
            .show();
}


private static boolean isCallable(Context context, Intent intent) {
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

public void hasProtectedApps(String packaGe, String activity) {
    try {
        String replacedActivityName = activity.replace(packaGe, "");
        //String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
        String cmd = "am start -n " + packaGe + "/" + replacedActivityName;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            cmd += " --user " + getUserSerial();
        }
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        if (BuildConfig.BUILD_TYPE.contains("release"))
            Crashlytics.logException(e);
        Log.e("isProtectedApps Error " + e.getMessage());

    }
}

private String getUserSerial() {
    //noinspection ResourceType
    Object userManager = getSystemService("user");
    if (null == userManager) return "";

    try {
        Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
        Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
        Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
        Long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
        if (userSerial != null) {
            return String.valueOf(userSerial);
        } else {
            return "";
        }
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
        if (BuildConfig.BUILD_TYPE.contains("release"))
            Crashlytics.logException(e);
        Log.e("getUserSerial Error " + e.getMessage());
    }
    return "";
  }
}
Ivor
  • 578
  • 1
  • 11
  • 27
  • How to use it please for head-up notification ? – baderkhane May 14 '18 at 18:42
  • Just call the function static method before to check the availability. – Ivor Jun 24 '18 at 23:07
  • Is a Java Class if you dont know what static method you have to call or how you should first look in Google, WhiteListUtils.checkOSCompat(CONTEXT); – Ivor Jul 05 '18 at 21:27
  • 1
    OOOH oky oky, i know now what you was talking about, many thanks ;) – baderkhane Aug 01 '18 at 11:22
  • @Ivor Why would adding the app to `Autostart` make any difference regarding notifications? – Manuel Dec 19 '19 at 13:15
  • You mean for Xiomi devices?, I add that so it triggers the request of adding the app to the whitelisting, this is made using an Intent created for each manufacturer launching the properly Activity to change the whitelisting configuration. – Ivor Dec 20 '19 at 12:11
  • The reasons is so it asks for not optimising (Doze), the app and keep receiving notifications, is that your question? @Manuel – Ivor Jul 15 '20 at 08:07