FCM not working push notification sound android

12,734

I think you are using the notification message type which only shows a notification when the app is in the foreground. You need to use data message type to get notifications (and sounds, vibrations, etc) when the app is in the background. Check my answer here.

Share:
12,734
Herman Andres Figueroa
Author by

Herman Andres Figueroa

my name is herman and I like to program, but I'm in training I hope to learn more and more my touch is [email protected]

Updated on June 04, 2022

Comments

  • Herman Andres Figueroa
    Herman Andres Figueroa almost 2 years

    I'm trying to send a push notification to my phone using Firebase console , but when the application is minimized, the sound and the title of the notification does not work does not work, and when the application is in the foreground working properly , what is the reason that the behavior notifications be different ?

    enter image description here

    MyFirebaseMessagingService.java

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, DashboardActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_info_black_24dp)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    

    AndroidManifest.xml

        <!-- [START firebase_service] -->
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_service] -->
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />