MIUI 推送通知没有声音

2023-12-23

我的应用程序的主要功能是从远程服务器发送的推送通知消息。我使用 FCM 作为消息传递服务。我的问题是,小米 9 Lite(Android 9/MIUI 11)上的通知没有任何声音。然而,在小米红米 Note 5 (Android 9/MIUI 10) 上声音效果很好,在三星 Galaxy S7 Edge (Android 8) 上也是如此。我创建了 MessagingService,它扩展了 FirebaseMessagingService 和通知通道,如文档中所写。

这是我的代码:

public class MessagingService extends FirebaseMessagingService {

    private static String channelId;
    private NotificationManager notificationManager;
    private NotificationChannel notificationChannel;
    private NotificationCompat.Builder notificationBuilder;

    private MessagesViewModel viewModel;

    public MessagingService() { }

    @Override
    public void onCreate() {
        super.onCreate();
        channelId = getResources().getString(R.string.default_notification_channel_id);
        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        final Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        notificationBuilder = new NotificationCompat.Builder(this, channelId);
        notificationBuilder.setSmallIcon(R.raw.metrial_message_icon);
        notificationBuilder.setAutoCancel(false);
        notificationBuilder.setSound(soundUri);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            String name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_HIGH;
            notificationChannel = new NotificationChannel(channelId, name, importance);
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true);
            notificationChannel.setShowBadge(true);
            notificationChannel.setSound(soundUri, audioAttributes);
            notificationManager.createNotificationChannel(notificationChannel);
            notificationBuilder.setChannelId(channelId);
        }
        else {
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
            notificationBuilder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
            notificationBuilder.setLights(Color.WHITE, 500, 5000);
        }

        viewModel = new MessagesViewModel(getApplication());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        logger.info("onNewToken()");
        ConnectionParameters.getInstance().setToken(s);
        MyPrefs.getInstance(getApplicationContext()).putString(Constants.TOKEN, s);
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        final String messageId = remoteMessage.getData().get("message_id");
        final String title = remoteMessage.getData().get("title");
        final String body = remoteMessage.getData().get("body");

        if (messageId != null && title != null && body != null) {

            final Message message = new Message();
            message.setMessageId(messageId);
            message.setTitle(title);
            message.setContent(body);
            message.setTimestamp(new Date());

            try {
                message.setNotificationId((int)viewModel.insert(message));
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }

            logger.info("onMessageReceived(): notificationId=" + message);

            if (MyPrefs.getInstance(getApplicationContext()).getBoolean(Constants.ENABLE_PUSH)) {
                notificationBuilder.setContentTitle(title);
                notificationBuilder.setContentText(body);

                final Intent notifyIntent = new Intent(this, MessageInfoActivity.class);
                notifyIntent.putExtra(Constants.ARG_MESSAGE_OBJECT, message);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
                stackBuilder.addNextIntentWithParentStack(notifyIntent);
                PendingIntent pendingActivityIntent =
                        stackBuilder.getPendingIntent(message.getNotificationId(), PendingIntent.FLAG_UPDATE_CURRENT);
                notificationBuilder.setContentIntent(pendingActivityIntent);

                final Notification notification = notificationBuilder.build();
                notification.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
                notificationManager.notify(message.getNotificationId(), notification);
            }
        }
    }

    private final Logger logger = LoggerFactory.getLogger(getClass());
}

And in Settings->Notifications I got the following parameters: enter image description here

And inside my push-notifications-channel sound is enabled but whenever a message comes, it seems like app notification settings override parameters in notification channel. enter image description here

应该有一些解决方案,因为在 WhatsApp、Telegram 等流行应用程序中,这些开关在安装后启用(默认情况下)。希望有人帮忙!


由于没有人提供更好的解决方案,我想没有办法在 MIUI(以及大多数其他中国 OEM)上以编程方式允许声音/徽章计数器/浮动通知。用户有权手动打开这些设置。因此,为了增强用户体验,尽可能减少“点击”的数量很重要。因此,我们可以提供一个对话框,描述如何使用通向应用程序设置的按钮来启用上述功能。 即,要通过Intent打开通知设置页面,请执行以下操作:

final Intent notificationSettingsIntent = new Intent();
notificationSettingsIntent
        .setAction("android.settings.APP_NOTIFICATION_SETTINGS");
notificationSettingsIntent
        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    notificationSettingsIntent.putExtra(
            "android.provider.extra.APP_PACKAGE",
            activity.getPackageName());
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationSettingsIntent.putExtra(
                "app_package",
                activity.getPackageName());
        notificationSettingsIntent.putExtra(
                "app_uid", 
                activity.getApplicationInfo().uid);
}
activity.startActivityForResult(
        notificationSettingsIntent, 
        NOTIFICATIONS_SETTINGS_REQUEST_CODE);

您可以打开一个带有“打开通知设置”按钮的对话框,单击该按钮会触发上面的代码片段。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MIUI 推送通知没有声音 的相关文章

  • 如何持续更新MPAndroidChart中的Y轴值

    我希望 LineChart 中的轴能够实时调整其最大值和最小值 当新数据的 Y 值增加 正值和负值 时 像 ResetAxisMaxValue 和 ResetAxisMinValue 这样的函数可以很好地工作 但是 一旦信号再次变低 Y 值
  • Java 数组的最大维数

    出于好奇 在 Java 中数组可以有多少维 爪哇language不限制维数 但是JavaVM规范将维度数限制为 255 例如 以下代码将无法编译 class Main public static void main String args
  • 获取给定类文件的目录路径

    我遇到的代码尝试从类本身的 class 文件所在的同一目录中读取一些配置文件 File configFiles new File this getClass getResource getPath listFiles new Filenam
  • 无法获取 Facebook 传入请求

    我正在尝试在我的 Facebook android 游戏应用程序中实现发送数据并接受该数据 我正在关注https developers facebook com docs android send requests notification
  • BottomSheetDialog get Behavour 始终返回 null

    我与底部表单对话框我必须获得行为才能设置setBottomSheetCallback 来处理一些事情 As 谷歌说 https android developers googleblog com 2016 02 android suppor
  • Android 从命令行停止模拟器

    这个问题与如何通过命令行关闭Android模拟器 https stackoverflow com questions 5912403 how to shut down android emulator via cmd 但是 在尝试第一个答案
  • Android - 如何更改 TimePicker 中的文本颜色?

    我正在使用 TimePicker 到 LinearLayout 中 背景颜色 黑色 但是 我看不到 TimePicker 中的数字 并且我需要在布局中将背景颜色设置为黑色 如何更改 TimePicker 中的 textColor 我已经尝试
  • Espresso 和 Proguard 的 Java.lang.NoClassDefFoundError

    我对 Espresso 不太有经验 但我终于成功地运行了它 我有一个应用程序需要通过 Proguard 缩小才能处于 56K 方法之下 该应用程序以 3 秒的动画开始 因此我需要等到该动画结束才能继续 这就是我尝试用该方法做的事情waitF
  • 在为 Android 实现 Google 登录时,任务“:app:transformClassesWithDexForDebug”执行失败

    我正在尝试为 Android 实现 Google 登录 并且我正在按照以下说明进行操作 https developers google com identity sign in android start integrating https
  • Linux 上有关 getBounds() 和 setBounds() 的 bug_id=4806603 的解决方法?

    在 Linux 平台上 Frame getBounds 和 Frame setBounds 的工作方式不一致 这在 2003 年就已经有报道了 请参见此处 http bugs java com bugdatabase view bug do
  • 对象锁定私有类成员 - 最佳实践? (爪哇)

    I asked 类似的问题 https stackoverflow com questions 10548066 multiple object locks in java前几天 但对回复不满意 主要是因为我提供的代码存在一些人们关注的问题
  • 解决错误javax.mail.AuthenticationFailedException

    我不熟悉java中发送邮件的这个功能 我在发送电子邮件重置密码时遇到错误 希望你能给我一个解决方案 下面是我的代码 public synchronized static boolean sendMailAdvance String emai
  • 如何在Java中正确删除数组[重复]

    这个问题在这里已经有答案了 我刚接触 Java 4 天 从我搜索过的教程来看 讲师们花费了大量精力来解释如何分配二维数组 例如 如下所示 Foo fooArray new Foo 2 3 但我还没有找到任何解释如何删除它们的信息 从内存的情
  • FCM onMessageReceived 应用程序运行时返回空白消息和标题

    正如您在标题中所写 当应用程序关闭时 它运行良好 并且onMessageReceived获取消息正文和标题 但如果应用程序处于前台模式 运行模式 则可以发送通知 但没有消息和标题 请问该怎么办 代码 Override public void
  • 安卓的限制

    我需要构建一个应用程序 该应用程序拍摄相机图像并将其上传到网络 在网络上进行一些处理并返回真 假 我在这方面遇到了一些问题 希望得到澄清 1 我的应用程序有什么方法可以知道 Android 相机捕获的图像吗 我从这里明白了什么 Androi
  • Android:解析 XML 数据的最佳解析器 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在开发一个应用程序 其中我第一次要解析来自远程服务器的 xml 文件中的数据 但我无法选择哪个解析器是有效的或最适合解析的 因为我知道主要有
  • 在没有 Wifi 的情况下获取 Android 设备的 MAC 地址

    如何获取没有 Wifi 接口的 Android 设备 例如 Android 模拟器 的网络接口的 MAC 地址 通过WifiManager返回获取的WifiInfonull EDIT 更清楚地说 我必须与本地网络上的现有网络协议 不是我设计
  • Java &= 运算符应用 & 或 && 吗?

    Assuming boolean a false 我想知道是否这样做 a b 相当于 a a b logical AND a is false hence b is not evaluated 或者另一方面 这意味着 a a b Bitwi
  • 嵌入式 Jetty - 以编程方式添加基于表单的身份验证

    有没有一种方法可以按如下方式以编程方式添加基于表单的身份验证 我用的是我自己的LdapLoginModule 最初我使用基本身份验证并且工作正常 但现在我想在登录页面上进行更多控制 例如显示徽标等 有没有好的样品 我正在使用嵌入式 jett
  • JAXB - 列表<可序列化>?

    我使用 xjc 制作了一些课程 public class MyType XmlElementRefs XmlElementRef name MyInnerType type JAXBElement class required false

随机推荐

  • Zend Framework:该行已标记为只读

    这是我第一次遇到这个问题 保存我的模型之一时 我收到错误消息 此行已标记为只读 不知道为什么我会收到此错误以及如何解决它 堆栈跟踪对我没有帮助 如何解决此问题以便保存记录 将行标记为只读可能是以下任一操作的结果 The Zend Db Se
  • Cocos2d中用手指旋转精灵

    我需要帮助用手指计算精灵的旋转 精灵旋转得很好 但在我手指第一次触摸时 它不知何故自行旋转了几度 此外 只有当手指围绕精灵中心旋转时 旋转才起作用 我正在尝试模拟自行车车轮 并有一个齿轮精灵和一个踏板精灵作为齿轮精灵的子级 我希望当我触摸踏
  • 输入类型=复位和敲除

    单击表单重置按钮时 Knockout 不会更新可观察值 http jsfiddle net nQXeM http jsfiddle net nQXeM HTML
  • 为什么 ARC 在 popViewController 之后不释放内存

    我在 UINavigationController 中推送和弹出 ViewController 我正在跟踪我的应用程序的内存消耗 在推送新的 viewController 时 内存消耗逐渐增加 但是当我使用以下命令弹出相同的 ViewCon
  • C++ 库实现如何分配内存但在程序退出时不释放它?

    代码相当简单 include
  • numpy:按列点积

    给定一个 2Dnumpy数组 我需要计算每一列与其自身的点积 并将结果存储在一个一维数组中 以下作品 In 45 A np array 1 2 3 4 5 6 7 8 In 46 np array np dot A i A i for i
  • 将表单数据序列化为 JSON [重复]

    这个问题在这里已经有答案了 我想对表单进行一些服务器前验证骨干网 js https en wikipedia org wiki Backbone js模型 为此 我需要将用户输入从表单获取为可用数据 我找到了三种方法来做到这一点 var i
  • JQuery:从“a”标签中删除“OnClick”事件

    这是一个奇怪的问题 我们的内网上有一些生产链接 一些 rouge javascript 在我们的 Intranet 主页上的所有链接上返回 false 我们无权访问源代码来重新构建控件并修复此 JavaScript 因此 作为临时创可贴 我
  • 为什么 scipy.stats.nanmean 给出与 numpy.nansum 不同的结果?

    gt gt gt import numpy as np gt gt gt from scipy import stats gt gt gt a np r 1 2 np nan 4 5 gt gt gt stats nanmean a 2 9
  • MySql SELECT 不同列的联合? [复制]

    这个问题在这里已经有答案了 我有一个选择查询 用于选择附加了缩略图文件的文件 并且我还需要获取那些未附加缩略图的文件 我当前的sql查询是 SELECT node title node nid files fid files filepat
  • 将 .txt 保存为 .csv 将取消宏在文件中所做的所有更改。如何预防呢?

    问题仍在继续this https stackoverflow com q 54523166 10348607主题并与之相关我之前的帖子 https stackoverflow com q 54488624 10348607 该代码应处理更改
  • Windows 2012 服务器 IIS 8.0 中的 IIS 调试

    我正在尝试使用 IIS 8 0 调试 Windows Server 2012 中托管的 Web 应用程序 我用代码打开VS2008并附加进程 但那里没有列出W3Wp exe进程 在任务管理器中我可以看到 w3wp exe 在details选
  • 将 JasperReport 导出到 PDF 输出流?

    我正在编写一个非常简单的示例项目来熟悉 Jasper Reports 我想将我配置的报告导出为 PDFOutputStream 但没有它的工厂方法 InputStream template JasperReportsApplication
  • 如何保存/恢复对象在 DOM 树中的位置?

    如果我有以下 html ul li test li li class draggable special li li test li ul 我该如何保存 draggable当前 DOM 位置 一般而言 我打算拖这个 draggable通过将
  • 类方法和实例方法的区别

    我正在读书PEP 8 https pep8 org 时尚指南 http www python org dev peps pep 0008 我注意到它建议使用self作为实例方法中的第一个参数 但是cls作为类方法中的第一个参数 我使用并编写
  • 检查 Cython 数组中是否存在值

    我想知道如何检查数组中是否存在值或对象 就像在 python 中一样 a 1 2 3 4 5 b 4 if b in a print True else print False 我想知道 cython 中是否已经存在类似的东西 我有一个指针
  • AWS Amplify 未经授权错误 - 未授权访问类型 [...]

    我有一个使用 google federate 登录的 AWS Amplify 应用程序 这是我的数据模型 type TriadeMetric model auth rules allow owner id ID NoteMetrics No
  • HTML5 离线谷歌地图访问

    我们正在开发一个 HTML5 应用程序 它有一个 Google 地图来更改搜索位置 现在这个应用程序也有离线版本 有什么办法可以缓存谷歌地图 当应用程序离线时 它会显示离线版本 我们怎样才能让用户再次更改位置呢 目前谷歌地图还没有任何缓存机
  • 通过 PHP 上传 ZIP 文件并解压缩 ftp 文件夹

    我想制作一个表格 您可以在其中填写 FTP 登录服务器并获得上传 ZIP 文件的选项 该脚本与最后一部分 解压缩文件 分开工作 我想执行解压缩上传的文件 有谁知道有什么问题吗 TIA
  • MIUI 推送通知没有声音

    我的应用程序的主要功能是从远程服务器发送的推送通知消息 我使用 FCM 作为消息传递服务 我的问题是 小米 9 Lite Android 9 MIUI 11 上的通知没有任何声音 然而 在小米红米 Note 5 Android 9 MIUI