类似小米和乐视设备的提醒通知

Posted

技术标签:

【中文标题】类似小米和乐视设备的提醒通知【英文标题】:Heads-up notification for xiaomi and LeEco like devices 【发布时间】:2017-09-20 07:51:17 【问题描述】:

我正在处理提醒通知。我的代码适用于所有设备,但不适用于 android 5.1 以上的小米和乐视之类的设备。 我的代码是:

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

可能是什么问题?

【问题讨论】:

您有任何错误,请在此处发布。 我没有收到任何错误但没有收到提示通知,它是面板中的正常通知。我检查了更多,这些设备有更多的通知权限。他们对其进行了更严格的限制。我需要手动打开这些检查,然后我会收到提示通知。就像乐视有“横幅通知检查”。 【参考方案1】:

我认为这个解决方案对你很有帮助,但是我找不到任何乐视设备的解决方案:

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

【讨论】:

抬头通知如何使用? 在检查可用性之前只需调用函数静态方法即可。 如果你不知道你必须调用什么静态方法或者你应该如何首先在 Google 中查看,那么它是一个 Java 类,WhiteListUtils.checkOSCompat(CONTEXT); OOOH 好的好的,我现在知道你在说什么了,非常感谢 ;) @Ivor 为什么将应用添加到Autostart 会对通知产生任何影响?

以上是关于类似小米和乐视设备的提醒通知的主要内容,如果未能解决你的问题,请参考以下文章

安卓设备的 GCM 推送通知不适用于 MI 和乐视手机

如何在小米设备中以编程方式允许通知声音

iOS 中的离线通知

在设备离线时响应本机应用程序发送推送通知

iQOO 手机如何关闭电量过低通知?

Socket,长连接,消息推送,消息提醒,未读消息提醒,消息通知,未读消息通知