Android Firebase 通知没有声音
Android Firebase notification have no sound
我在应用程序运行时收到 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());
}
请帮我解决这个问题。
notificationChannel.setSound(null,null); // remove this line
.setSound(sound) //replace this line with this
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
只需做一件事,让您的后端开发人员在通知中发送 data payload,要求他限制并删除 notification payload ,
因为当您当时收到通知时,如果您的应用程序在后台并且您当时收到通知有效负载,系统会处理来自他们这边的通知,这就是问题出现的原因,
所以只需从服务器端删除 notification 负载,它就可以正常工作。
添加您的 php 数据代码,如下所示
$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
'registration_ids' => ['deviceID'],
'priority' => 'high',
'data' => array(
'body' => $message,
'title' => $title,
'sound' => 'default',
'icon' => 'icon'
)
);
如果您使用的是 FCM,则不需要实现通知代码,除非您需要为您的应用自定义它(例如特殊灯光、特殊声音、特殊振动和...)。
否则,你可以像这样 class 并设置清单的东西
FCM Class:
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(Objects.requireNonNull(remoteMessage.getNotification()).getTitle(), remoteMessage.getNotification().getBody());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "net.Test.id";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableLights(true);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(title).setContentText(body).setContentInfo("Info");
assert notificationManager != null;
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("TOKEN", s);
}
}
清单飞行:
<service
android:name="arbn.rahyab.rahpayadmin.data.network.fcm.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/googleg_standard_color_18" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="001" />
如果要自定义,在FCN里面写代码class。
我在应用程序运行时收到 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());
}
请帮我解决这个问题。
notificationChannel.setSound(null,null); // remove this line
.setSound(sound) //replace this line with this
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
只需做一件事,让您的后端开发人员在通知中发送 data payload,要求他限制并删除 notification payload ,
因为当您当时收到通知时,如果您的应用程序在后台并且您当时收到通知有效负载,系统会处理来自他们这边的通知,这就是问题出现的原因,
所以只需从服务器端删除 notification 负载,它就可以正常工作。
添加您的 php 数据代码,如下所示
$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
'registration_ids' => ['deviceID'],
'priority' => 'high',
'data' => array(
'body' => $message,
'title' => $title,
'sound' => 'default',
'icon' => 'icon'
)
);
如果您使用的是 FCM,则不需要实现通知代码,除非您需要为您的应用自定义它(例如特殊灯光、特殊声音、特殊振动和...)。 否则,你可以像这样 class 并设置清单的东西
FCM Class:
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(Objects.requireNonNull(remoteMessage.getNotification()).getTitle(), remoteMessage.getNotification().getBody());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "net.Test.id";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableLights(true);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(title).setContentText(body).setContentInfo("Info");
assert notificationManager != null;
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("TOKEN", s);
}
}
清单飞行:
<service
android:name="arbn.rahyab.rahpayadmin.data.network.fcm.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/googleg_standard_color_18" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="001" />
如果要自定义,在FCN里面写代码class。