【问题标题】:FCM custom notification sound not workingFCM 自定义通知声音不起作用
【发布时间】:2019-03-24 19:23:55
【问题描述】:

我知道以前有人问过这个问题,我检查了所有其他帖子并尝试了所有帖子,但没有一个有效。基本上,我有一个 mp3 文件,当用户收到通知时我想播放它,

这是我的代码

    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());
    }
}

【问题讨论】:

  • 您是使用 Firbase 控制台还是通过服务器发送通知?
  • 我测试了服务器和firebase,都没有工作

标签: android firebase firebase-cloud-messaging


【解决方案1】:

我必须做的是卸载并重新安装应用程序,然后它就可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 2023-03-28
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2021-11-15
    相关资源
    最近更新 更多