Android上退出和后台状态下,在推送通知中使用系统声音而非自定义声音。
创始人
2024-10-10 07:01:22
0

在AndroidManifest.xml文件中,为进程指定一个默认通知声音。可以使用以下代码:


    

其中,@raw/my_custom_sound是在/res/raw目录下的自定义声音文件。如果不指定自定义声音,则默认使用系统通知声音。需要注意的是,默认通知声音仅在推送通知中使用,而非本地通知中。

此外,还需在FirebaseMessagingService的onMessageReceived方法中添加以下代码,以确保在推送通知中使用默认通知声音:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    if (remoteMessage.getNotification().getSound() == null) {
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(defaultSoundUri);
    }

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(notificationId, builder.build());
}

在上述代码中,如果推送通知中未指定声音,则使用默认通知声音。如果指定了声音,则将推送通知中指定的声音用于通知。

相关内容

热门资讯

黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(扑克之城)好像是真的有挂,攻略教程(有挂方法)是一款可以...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(菠萝德州软件)原先是真的有挂,黑科技教程(详细教程);菠萝德...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德州ai盈利)一直是有挂,AI教程(有挂讲解);德州ai...
黑科技辅助!wepoke透明黑... 黑科技辅助!wepoke透明黑科技,(AAPoKer模拟器)往昔是有挂,科技教程(真的有挂);AAP...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件安装,(微扑克机器人)总是存在有挂,揭秘教程(果真有挂);微扑克机器...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(德扑之星代打)都是真的有挂,必胜教程(今日头条),德扑之...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(AAPoKerai机器人)真是是有挂,力荐教程(有挂辅助...
黑科技辅助!wepoke有挂真... 黑科技辅助!wepoke有挂真的有挂,(红龙扑克机制)原来有挂,高科技教程(有挂教程);亲,有的,a...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件,(WePoKe黑科技)先前真的是有挂,2025新版教程(发现有挂)...
黑科技辅助!wepoke智能a... 黑科技辅助!wepoke智能ai辅助工具,(悟空黑桃a德州)先前是有挂,解说技巧(有挂秘笈);1、让...