android - 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());
}
请帮我解决一下这个。
解决方案
只需做一件事,说您的后端开发人员只需在通知中发送数据有效负载,要求他限制和删除通知有效负载,
因为当您当时收到通知时,如果您的应用程序在后台并且您当时收到通知有效负载,系统会处理来自他们这边的通知,这就是出现问题的原因,
因此,只需从服务器端删除通知有效负载,它就可以正常工作。
添加您的 php 代码,如下所示以获取数据
$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
'registration_ids' => ['deviceID'],
'priority' => 'high',
'data' => array(
'body' => $message,
'title' => $title,
'sound' => 'default',
'icon' => 'icon'
)
);
推荐阅读
- android - FCM - 以不同用户身份登录后无法接收从同一设备发送的通知
- r - 是否可以在不重新创建或修改几何图层的情况下添加 ggplot 美学?
- tensorflow - Tensorflow:估计器中没有提供 mean_squared_error 的梯度
- ios - 为什么在 didFailWithError 后我会收到错误的 GPS 位置?
- jquery - jQuery、DataTables、KeyTables:使用空格键选中复选框
- tfs - TF246017: 更改域帐户密码后,Team Foundation 服务器无法连接到数据库
- neural-network - 训练全卷积网络时忽略标签
- javascript - React - 单击 div 文本,从另一个 div 更改文本
- trace32 - 劳特巴赫宏的懒惰评价——有可能吗?
- c++ - 将 STL 文件添加到 QT Widget