我已经为推送通知实施 FCM。我正在调用此 OnMessageReceived 方法,但是当应用程序位于前台和后台时,不会播放声音
这是我的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String ChannelID = "";
public static final int NOTIFICATION_ID = 1041;
public static boolean IsDriverBusy = false;
private NotificationManager notificationManager;
private SharedPreferences sharedPreferences;
private NotificationChannel mChannel;
private NotificationCompat.Builder notificationBuilder;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
System.out.println("notificationIntentIs 0000000000000123545 onMessageReceived " + IsDriverBusy);
String mTime = getDate(remoteMessage.getSentTime());
String RiderLOcation = remoteMessage.getData().get("RiderLocation");
String RiderID = remoteMessage.getData().get("RiderID");
String Dlat = remoteMessage.getData().get("Dlat");
String DLong = remoteMessage.getData().get("DLong");
String Plat = remoteMessage.getData().get("Plat");
String Plong = remoteMessage.getData().get("Plong");
String RiderName = remoteMessage.getData().get("RiderName");
String RiderRating = remoteMessage.getData().get("RiderRating");
String MainBody = "RiderID:" + RiderID + ";RiderName:" + RiderName + ";RiderLocation:"
+ RiderLOcation + ";PLat:" + Plat + ";PLong:" + Plong + ";DLat:" + Dlat + ";DLong:" + DLong + ";RiderRating:" + RiderRating;
sharedPreferences = getSharedPreferences("LoginDetail", Context.MODE_PRIVATE);
if (sharedPreferences.getString("id", "").equals("")) {
Intent intent1 = new Intent(this, LoginActivity.class);
startActivity(intent1);
} else {
if (!IsDriverBusy) {
sendNotification(MainBody);
PreferenceHandler.writeString(this, PreferenceHandler.RIDE_DETAIL, MainBody);
}
}
}
private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time * 1000L);
String date = DateFormat.format("dd-MM-yyyy hh:mm:ss", cal).toString();
return date;
}
@SuppressLint("WrongConstant")
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("bookingId", messageBody);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
startActivity(intent);
ChannelID=getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = Uri.parse("android.resource://com.my.package/" + R.raw.windchime);
long[] VIBRATE_PATTERN = {0, 500};
notificationBuilder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.logo)
.setVibrate(VIBRATE_PATTERN)
.setPriority(Notification.DEFAULT_ALL)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getString(R.string.notificationData))
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
AudioAttributes mAudioAttributes=new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mChannel = new NotificationChannel(ChannelID, "Qwykr_Channel",
NotificationManager.IMPORTANCE_HIGH);
mChannel.setVibrationPattern(VIBRATE_PATTERN);
mChannel.setImportance(Notification.DEFAULT_ALL);
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,mAudioAttributes);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
clearNotification(NOTIFICATION_ID);
}
public void clearNotification(int ID) {
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
}
};
Runnable doDisplayError = new Runnable() {
public void run() {
notificationManager.cancel(ID);
}
};
handler.postDelayed(doDisplayError, 3000);
}
}
我正在使用小米Redmi 7 pro。我正在检查手机设置应用程序通知通道,唯一的振动启用而不是声音,并且启用了另一个选项。如何设置像 Whatsapp 和电报应用程序这样的频道?如何启用屏幕截图的所有字段,如声音、浮动通知、锁屏通知?这是我的应用频道截图:
请帮我解决问题。提前感谢