首页 > 解决方案 > Android Firebase 通知没有声音

问题描述

我在应用程序运行时收到 Firebase 通知声音,而在应用程序处于后台时没有收到通知声音。我不知道为什么会这样。

这是我尝试过的

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
            // To Configure the notification channel.
            notificationChannel.setDescription("Sample Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.gj512x512)
                .setContentText(message)
                .setSound(sound)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        notificationManager.notify(1, noBuilder.build());
}

请帮我解决一下这个。

标签: androidfirebasenotificationsfirebase-cloud-messaging

解决方案


只需做一件事,说您的后端开发人员只需在通知中发送数据有效负载,要求他限制和删除通知有效负载,

因为当您当时收到通知时,如果您的应用程序在后台并且您当时收到通知有效负载,系统会处理来自他们这边的通知,这就是出现问题的原因,

因此,只需从服务器端删除通知有效负载,它就可以正常工作。

添加您的 php 代码,如下所示以获取数据

$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
    'registration_ids'  => ['deviceID'],
    'priority' => 'high',
    'data' => array(
        'body' => $message,
        'title' => $title,
        'sound' => 'default',
        'icon' => 'icon'
    )
);

推荐阅读