简体   繁体   中英

Android Firebase notification have no sound

I'm getting Firebase notification sound while app runs and not getting notification sound while app is in background. I don't know why it is happening.

This is what I tried

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
            // To Configure the notification channel.
            notificationChannel.setDescription("Sample Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.gj512x512)
                .setContentText(message)
                .setSound(sound)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        notificationManager.notify(1, noBuilder.build());
}

Please help me with this.

Just do one thing say your backend developer to just send data payload in the notification, ask him to restrict and remove notification payload,

Because when you are getting a notification at that time if your app in the background and you are getting notification payload at that time system handle notification from their side and that's why problem arise,

So simply just remove notification payload from the server-side and it will work fine.

Add your php code like bellow for data

$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
    'registration_ids'  => ['deviceID'],
    'priority' => 'high',
    'data' => array(
        'body' => $message,
        'title' => $title,
        'sound' => 'default',
        'icon' => 'icon'
    )
);
 notificationChannel.setSound(null,null);  // remove this line 



.setSound(sound) //replace this line with this 

setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 

if you're using FCM, you don't need to implement notification codes, unless you need to customize it for your app (for example special light, special sound, special vibration and...). otherwise, you can just have a class like this and set manifest's things too

FCM Class:

public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    showNotification(Objects.requireNonNull(remoteMessage.getNotification()).getTitle(), remoteMessage.getNotification().getBody());
}

private void showNotification(String title, String body) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "net.Test.id";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setDescription("Description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableLights(true);
        assert notificationManager != null;
        notificationManager.createNotificationChannel(notificationChannel);
    }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle(title).setContentText(body).setContentInfo("Info");
    assert notificationManager != null;
    notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("TOKEN", s);
}
}

manifest flie:

<service
        android:name="arbn.rahyab.rahpayadmin.data.network.fcm.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/googleg_standard_color_18" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="001" />

if you want to customize it, write codes inside FCN class.

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