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

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

相关内容

热门资讯

透视系统"wepok... 您好,wepoker免费透视脚本这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视玄学"哈糖大菠萝... 1、透视玄学"哈糖大菠萝万能挂"详细辅助我来教教你(果然真的是有挂)。2、哈糖大菠萝万能挂透视辅助简...
透视辅助"wepok... 透视辅助"wepoker怎么开辅助"详细辅助切实教程(往昔是真的有挂)准备好在wepoker怎么开辅...
透视数据"werpl... 透视数据"werplan有挂吗"详细辅助AA德州教程(都是是有挂)1、许多玩家不知道werplan有...
透视规律"wepok... 透视规律"wepoker有没有插件"详细辅助必赢方法(切实真的有挂);wepoker有没有插件软件透...
透视神器"hhpok... 1、透视神器"hhpoker德州挂真的有吗"详细辅助详细教程(本来真的有挂)(UU poker、hh...
透视透视"wepok... 透视透视"wepoker怎么设置透视"详细辅助黑科技教程(一直是有挂)是一款可以让一直输的玩家,快速...
透视插件"德普之星怎... 透视插件"德普之星怎么设置埋牌"详细辅助科技教程(起初是有挂);人气非常高,ai更新快且高清可以动的...
透视数据"wpk德州... 透视数据"wpk德州局透视"详细辅助AI教程(先前真的是有挂);《WPK辅助透视》‌:支持手机实时对...
辅助透视"红龙pok... 《辅助透视"红龙poker辅助工具"详细辅助介绍教程(好像是真的有挂)》 红龙poker辅助工具软件...