通知" setSound()"在Android 8中

时间:2018-06-15 03:03:41

标签: notifications android-8.0-oreo

问题在于,通知代码无法与Android 8配合使用。 我不能自己设置通知声音。它显示的唯一结果是系统声音" Bee"。

这是我的代码:

if(Build.VERSION.SDK_INT>=26) {
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String id = "channel_1";
    String description = "123";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
    mChannel.enableLights(true);
    mChannel.enableVibration(true);
    mChannel.setLightColor(Color.GREEN);
    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    AudioAttributes aa = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .build();


   mChannel.setSound(Uri.parse("android.resource://com.example.lenovo.projectmonitor/" + R.raw.video11),aa);

   manager.createNotificationChannel(mChannel);
   Notification notification = new NotificationCompat.Builder(NotificationService.this, "channel_1")
                        .setContentTitle("new alarm")
                        .setContentText(nameAll.get(0))
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setContentIntent(pi)
                        .setAutoCancel(true)
                        .build();

    manager.notify(1, notification);
}

1 个答案:

答案 0 :(得分:0)

在NotificationChannel和NotificationCompat.Builder类中使用setSound方法。例子

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.channel_name);
        String description = context.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(channelId, name, importance);
        channel.setDescription(description);
        channel.setSound(null, null);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

并创建buileder:

        public void buildNotification(Class className, int smallIconId) {
        createNotificationChannel();
        // Create an Intent for the activity you want to start
        Intent intent = new Intent(context, className);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
//        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//        stackBuilder.addNextIntentWithParentStack(intent);
//// Get the PendingIntent containing the entire back stack
//        PendingIntent pendingIntent =
//                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder
                .setSmallIcon(smallIconId)
                .setContentTitle("Recording Audio..")
//                .setContentText("Much longer text that cannot fit one line...")
//                .setStyle(new NotificationCompat.BigTextStyle()
//                        .bigText("Much longer text that cannot fit one line..."))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(null)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);


        // notificationId is a unique int for each notification that you must define
//            notificationManager.notify(notificationId, mBuilder.build());

//        mBuilder.setContentTitle("changed it");
//        notificationManager.notify(notificationId, mBuilder.build());
//            mHandler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                }
//            }, 3000);
    }