简体   繁体   中英

No sound sent with the FCM notification

I made my application send notifications when a user sends you a message but when the notification is shown the sound doesn't play. Bellow is the code of my app.

MyFirebaseMessaging:

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

What may be the issue? I thought that I may forgot to trigger the Uri on MyFirebaseMessaging but I did it. I tried to add a custom sound too because I thought it may be a bug with the default sound but nothing changed. Is there another way to make it happen?

The below code snippet might help you sort out the problem. This snippet is producing a default notification sound whenever a notification has arrived.

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

.setSound(soundUri) does nothing on Android OREO and newer.

For newer Android versions the notifications sound depends on the Channel and is configured in Android settings Apps >> your app >> Notifications

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM