簡體   English   中英

MIUI無聲推送通知

[英]Push notifications with no sound on MIUI

我的應用程序的主要功能是從遠程服務器發送的推送通知消息。 我使用 FCM 作為消息傳遞服務。 我的問題是小米 Mi 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());
}

Settings->Notifications我得到以下參數: 在此處輸入圖像描述

並且在我的推送通知通道中啟用了聲音,但是每當收到消息時,似乎應用程序通知設置會覆蓋通知通道中的參數。 在此處輸入圖像描述

應該有一些解決方案,因為在 WhatsApp、Telegram 等流行應用程序中,這些開關在安裝后啟用(默認情況下)。 希望,有人幫助!

由於沒有人提供更好的解決方案,我想沒有辦法在 MIUI(以及主要在其他中國 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);

您可以使用“打開通知設置”按鈕打開一個對話框,單擊該按鈕會觸發上面的代碼片段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM