【问题标题】:Android ; Sound not playing in FCM push notifications安卓 ; FCM 推送通知中未播放声音
【发布时间】:2018-11-18 04:52:49
【问题描述】:

我正在从调用 firebase api 的应用服务器向用户发送数据消息。用户正在接收通知,但通知到达时没有播放声音。

这里是代码

public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    sendNotification(remoteMessage); 
}

private void sendNotification(RemoteMessage message) {
    Intent intent;
    intent = new Intent(this, HomeActivity.class);
    Log.d(TAG, message.getData().toString());

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
    r.play();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(message.getData().get("title"))
            .setContentText(Html.fromHtml(message.getData().get("body")))
            .setAutoCancel(true)
//                .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
        saveMessage(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

可能是什么问题???

【问题讨论】:

  • 您可以在此链接中查看您的解决方案:stackoverflow.com/questions/37959588/…
  • 这意味着我需要在发送给用户的数据消息中发送参数“声音”:“默认或自定义”。
  • 您是否尝试过设置默认值?即setDefaults(Notification.DEFAULT_SOUND)?

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


【解决方案1】:

您需要在通知生成器上设置声音。 因此,请尝试在下面的行中取消注释。

.setSound(defaultSoundUri)

并在下面的行注释。

//Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
//r.play(); 

如果您想在应用程序处于后台时播放通知声音,您需要add the sound parameter 到通知负载。

支持应用中捆绑的声音资源的“默认”文件名。声音文件必须驻留在/res/raw/

【讨论】:

  • 我第一次尝试了 .setSound(defaulturi) 但仍然没有播放...是否需要在发送给用户的数据消息中设置 Sound 参数..
  • 我正在发送数据消息..即使我的应用程序在前台,声音也没有播放...
猜你喜欢
  • 2022-07-12
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
相关资源
最近更新 更多