【问题标题】:Android: custom sound not playing in FCM push notificationsAndroid:自定义声音未在 FCM 推送通知中播放
【发布时间】:2022-07-12 22:07:00
【问题描述】:

请注意,我们正在尝试向用户的 Android 设备发送带有自定义声音的推送通知。从我们的应用程序使用 FCM 发送推送通知时,消息确实到达了用户的设备,但负载中设置的自定义声音不会播放。通知到达,但自定义声音不播放,默认通知声音也不播放。 这是我们正在使用的有效负载

 $notification = [
            'title' =>"Hello",
            'body' =>"Hello",
            'image'=>''
            'sound'=>'https://mbracecloud.com/sound.mp3'
            ];
      

Firebase.java:NotificationCompat.Builder

Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
        Notification noti = notificationBuilder.build();
        noti.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(notification_id, notificationBuilder.build());

请指导我们哪里出错。

【问题讨论】:

    标签: android push-notification notifications firebase-cloud-messaging


    【解决方案1】:

    为了接收带有声音的推送通知,推送数据应包含声音参数, 这是推送数据的示例:

    {
      "token": "client_notification_token", <- or topic
      "notification": {
        "title": "Push notification title",
        "body": "Push body",
        "sound": "filename", <-- points to src/res/raw/filename.mp3
      }
      ...
    }
    

    如上例所示,您需要将声音文件(filename.mp3)保存在 res > raw 文件夹中。

    现在让我们来看看如何处理这个声音文件的编码部分。 我正在 NotificationChannel 对象中处理声音。

    mChannel.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.waiting_ringtone)
    

    我希望这会有所帮助,如果有帮助,请点赞并标记为已回答

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多