在Oreo Android中没有推送通知声音

时间:2019-06-02 05:09:10

标签: java android android-studio

我正在使用以下代码进行推送通知,但是在oreo上我没有收到任何声音。

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 个答案:

没有答案