java - MIUI推送通知没有声音

标签 java android push-notification miui

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

这是我的代码:

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

设置->通知中我得到了以下参数: enter image description here

并且在我的push-notifications-channel 中启用了声音,但每当有消息出现时,应用程序通知设置似乎会覆盖通知 channel 中的参数。 enter image description here

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

最佳答案

由于没有人提供更好的解决方案,我想没有办法在 MIUI 上以编程方式允许声音/徽章计数器/ float 通知(主要是在其他中国 OEM 上)。手动打开这些设置是用户的特权。因此,要增强用户体验,重要的是尽可能减少“点击”的数量。因此,我们可以提供一个对话框,描述如何使用通向 App 设置的按钮启用上述功能。 即,要通过 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);

您可以打开一个对话框,其中包含“打开通知设置”按钮,点击该按钮会触发上面的代码 fragment 。

关于java - MIUI推送通知没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58715149/

相关文章:

java - 立面图案的优缺点

ios - 当应用程序在 iOS 中处于运行状态时检测推送通知选择

android - 违反流异常透明度 : Emission from another coroutine is detected

php - SyncML 与 Android 和 PHP Web 服务

ios - 使用 Firebase 通知定位单个设备 (swift)

java - 具有多行摘要的自定义 Android 通知 BigPicture 不适用于 API 25 和 API 26

java - 在 Android 上正确处理 Realm 实例

java - 如何从方法中提取依赖于类上下文的变量以避免重复代码?

java - 是否可以从 Java 应用程序以 GPU 模式运行 TensorFlow?

android - 使用 MODE_WORLD_READABLE 阅读 pdf 时出错?