Android FCM自定义通知声音不工作
我知道以前有人问过这个问题,我检查了所有其他帖子,都试过了,但没有一个有效。基本上,我有一个mp3文件,当用户收到通知时,我想播放它 这是我的密码Android FCM自定义通知声音不工作,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我知道以前有人问过这个问题,我检查了所有其他帖子,都试过了,但没有一个有效。基本上,我有一个mp3文件,当用户收到通知时,我想播放它 这是我的密码 我所要做的就是卸载并重新安装应用程序,它工作了。您是使用Firbase控制台还是通过服务器发送通知?我对服务器和firebase都进行了测试,都不起作用 public class MessageService extends FirebaseMessagingService { @Override public void
我所要做的就是卸载并重新安装应用程序,它工作了。您是使用Firbase控制台还是通过服务器发送通知?我对服务器和firebase都进行了测试,都不起作用
public class MessageService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),remoteMessage.getData().get("url"));
else
showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"),remoteMessage.getData().get("url"));
}
private void showNotification(String title, String body, String url) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("url",url);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
//Uri sound =Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.notification);
//Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/notification");
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/notification.mp3");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.alibapir.test";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_ali_bapir_round)
.setSound(sound)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info")
.setContentIntent(resultPendingIntent);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel= new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Ali Bapir notification");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationChannel.enableLights(true);
notificationChannel.setSound(sound , new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_ALARM).build());
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
}