I am using following code for my push notifications, however on oreo I am not receiving any sound.

private void sendNotification(String messageBody, Intent intent)
    {
        String transId;
        String msg = "";
        try
        {
            JSONObject jsonObject = new JSONObject(messageBody);
            if (Preferences.getPrefLanguage(MyApplication.getInstance()).equals("en"))
            {
                msg = jsonObject.getString(Constants.RESPONSE_KEY_MESSAGE);
            } else
            {
                if (jsonObject.has(Constants.RESPONSE_KEY_MESSAGE_ARABIC))
                    msg = jsonObject.getString(Constants.RESPONSE_KEY_MESSAGE_ARABIC);
                else
                    msg = jsonObject.getString(Constants.RESPONSE_KEY_MESSAGE);
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }

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

        //Uri uri= Uri.parse("android.resource://"+getPackageName()+"/raw/soft_alert.wav");
        Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        uri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.soft_alert);

        String channelId = getString(R.string.default_notification_channel_id);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(msg)
                        .setAutoCancel(true)
                        .setSound(uri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel channel = new NotificationChannel(channelId,
                    getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_DEFAULT);
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            channel.setSound(uri, att);

            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify((int) Calendar.getInstance().getTime().getTime() /* ID of notification */, notificationBuilder.build());
        //notificationManager.notify(Constants.PUSH_ID /* ID of notification */, notificationBuilder.build());
    }

0 Answers