【问题标题】:Custom Sound Not Playing Firebase Messaging Android自定义声音不播放 Firebase 消息 Android
【发布时间】:2018-07-23 12:59:49
【问题描述】:

我正在尝试在我的 android 应用程序中使用自定义通知声音。我已经检查了 stackoverflow 中的所有相关问题和答案,但我无法弄清楚为什么它没有在应用程序中播放我的自定义声音。我的通知代码如下所示。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    if (remoteMessage.getNotification() != null) {

    }
    if (remoteMessage.getData().size() > 0) {
        sendNotification(remoteMessage.getData().get("title"));
    }

}

@Override
public void onNewToken(String token) {

}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, SplashActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Context context = getBaseContext();
    String channelId = "raj";
    int sound_notification = context.getSharedPreferences("setting", MODE_PRIVATE).getInt("sound_notification", 1);
    Uri MyCustomSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/sound" + (sound_notification));
    //Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(messageBody)
                    .setAutoCancel(true)
                    .setSound(MyCustomSound)
                    .setContentIntent(pendingIntent);

    //notificationBuilder.setSound(MyCustomSound);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Raj",
                NotificationManager.IMPORTANCE_HIGH);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    if (notificationManager != null) {
        notificationManager.notify(0, notificationBuilder.build());
    }
}

我尝试使用 PHP 以及 Firebase 控制台从我的服务器发送通知,但它总是播放默认声音而不是我的自定义声音。让我知道是否有人可以帮助我解决这个问题。 谢谢

【问题讨论】:

标签: java android firebase firebase-cloud-messaging


【解决方案1】:
    notification.sound = Uri.parse("android.resource://" + getPackageName() + "/sound/" + R.raw.sound);
    mBuilder.setSound(Uri.parse("android.resource://" + getPackageName() + "/sound/" + R.raw.sound));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多