Home > Blockchain >  firebase push notification sound not working on android app in android studio
firebase push notification sound not working on android app in android studio

Time:08-17

I have implement Push notification firebase services class and add on my project and send notification API Request through so notification proper receive but notification sound work to app active condition only but it will be require all notification receive time.

any one this type condition face and resolve please suggest changes.

     String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
        String text = remoteMessage.getNotification().getBody();
        remoteMessage.getMessageType();
//       String channelId = remoteMessage.getNotification().getChannelId();
//        Log.d("channelId", "onMessageReceived: " channelId);
        final String CHANNEL_ID = "HEADS_UP_NOTIFICATION";
        NotificationChannel channel = new NotificationChannel(
                 CHANNEL_ID,
                "Heads Up notification",
                NotificationManager.IMPORTANCE_HIGH
        );
        getSystemService(NotificationManager.class).createNotificationChannel(channel);
        Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.demo)
                .setAutoCancel(true)
                .setVibrate(new long[] { 0,100,300,500,800,1000, 1000, 1000, 1000, 1000 })
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setVisibility(Notification.VISIBILITY_PUBLIC);
        //  custom notification
     /*   MediaPlayer mp;
        mp = MediaPlayer.create(PushNotificationServices.this, R.raw.short_notification_sound);
        mp.start();*/
        @SuppressLint("UnspecifiedImmutableFlag") PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class),  PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        notification.setContentIntent(contentIntent);
        NotificationManagerCompat.from(this).notify(1,notification.build());
        super.onMessageReceived(remoteMessage);

CodePudding user response:

Try to use setSound method instead of setDefaults.

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    val notification: Notification.Builder = Builder(this, CHANNEL_ID)
        .setContentTitle(title)
        .setContentText(text)
        .setSmallIcon(R.drawable.demo)
        .setAutoCancel(true)
        .setVibrate(longArrayOf(0, 100, 300, 500, 800, 1000, 1000, 1000, 1000, 1000))
        .setOngoing(true)
        .setPriority(Notification.PRIORITY_MAX)
    --> .setSound(defaultSoundUri)
        .setVisibility(Notification.VISIBILITY_PUBLIC)

CodePudding user response:

First You have uninstall your app then try again.

If this does not work for then try using below code

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = CommonUtill.random();
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(true)

                    .setSound(defaultSoundUri)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(messageBody).setBigContentTitle(getString(R.string.app_name)))
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE   "://"   getPackageName()   "/"   R.raw.notification);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(channelId,
                getString(R.string.app_name),
                NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel.
        mChannel.setDescription("desc");
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(sound, attributes); // This is IMPORTANT


        if (notificationManager != null)
            notificationManager.createNotificationChannel(mChannel);

    }
    notificationManager.notify(943/* ID of notification */, notificationBuilder.build());

This is reference link Android Push Notification Sound is missing (Not Working ) background and foreground on android

  • Related