-1

I know this question has been asked before, I check all the other posts and tried them all, but none of them worked. Basically, I have an mp3 file which I want to play when the user receives notification,

here is my code

    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());
    }
}
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
MoTahir
  • 863
  • 7
  • 22

1 Answers1

2

I had to do is uninstall and re-install the application and it worked.

Matthias
  • 4,481
  • 12
  • 45
  • 84
MoTahir
  • 863
  • 7
  • 22