java - FCM 通知未发送声音
问题描述
当用户向您发送消息时,我让我的应用程序发送通知,但是当显示通知时,声音不会播放。贝娄是我的应用程序的代码。
MyFirebase 消息传递:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.example.selfcial.Activities.MessageActivity;
import com.example.selfcial.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String sented = remoteMessage.getData().get("sented");
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null && sented.equals(firebaseUser.getUid())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendOreoNotification(remoteMessage);
}else {
sendNotification(remoteMessage);
}
}
}
private void sendOreoNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userId", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title, body, pendingIntent
,soundUri, icon);
int i = 0;
if (j > 0) {
i = j;
}
oreoNotification.getManager().notify(i, builder.build());
}
private void sendNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userId", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int i = 0;
if (j > 0) {
i = j;
}
notificationManager.notify(i, builder.build());
}
}
OreoNotification:
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.net.Uri;
import android.os.Build;
import com.example.selfcial.R;
public class OreoNotification extends ContextWrapper {
private static final String CHANNEL_ID = "some_id";
private static final String CHANNEL_NAME= "Selfcial";
private NotificationManager notificationManager;
public OreoNotification(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (notificationManager == null) {
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
return notificationManager;
}
@TargetApi(Build.VERSION_CODES.O)
public Notification.Builder getOreoNotification(String title,
String body,
PendingIntent pendingIntent,
Uri soundUri,
String icon) {
return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_notification)
.setSound(soundUri)
.setAutoCancel(true);
}
}
可能是什么问题?我以为我可能忘记触发 UriMyFirebaseMessaging
但我做到了。我也尝试添加自定义声音,因为我认为这可能是默认声音的错误,但没有任何改变。还有其他方法可以实现吗?
解决方案
下面的代码片段可能会帮助您解决问题。每当通知到达时,此代码段就会产生默认通知声音。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder ( this, NOTIFICATION_CHANNEL_ID );
notificationBuilder.setAutoCancel ( true )
.setStyle ( new NotificationCompat.BigTextStyle ().bigText ( remoteMessage.getNotification ().getBody () ) )
.setDefaults ( Notification.DEFAULT_ALL )
.setWhen ( System.currentTimeMillis () )
.setSmallIcon ( R.drawable.ic_notification_icon )
.setTicker ( remoteMessage.getNotification ().getTitle () )
.setPriority ( Notification.PRIORITY_MAX )
.setContentTitle ( remoteMessage.getNotification ().getTitle () )
.setContentText ( remoteMessage.getNotification ().getBody () )
.setContentIntent ( contentIntent );
notificationManager.notify ( 1, notificationBuilder.build () );
推荐阅读
- node.js - 在 Docker 中运行时,`npm install` 导致`cb() never called!`
- cmd - 命令行复制特定名称模式的所有网络文件夹文件,包括子文件夹中的文件
- java - 需要日期为东部时区,并且使用 Java 1.7 将 AM 和 PM 添加到末尾
- javascript - Javascript:更聪明的方法来过滤数千行与数千个值
- list - 使用 Prolog 在一个列表中收集方法的结果
- kubernetes - 使用 kubeadm 设置 kubernetes HA - 无法启动调度程序和控制器
- solidity - Solidity 中的大循环
- visual-studio - Visual Studio 2017“脚本编辑器”拒绝扩展名为“.vbs”的文件
- c# - LInq 包含运算符显示设计时错误
- vivado - 如何在 Vivado 上合成 Rocket-Chip?