Android8.0及以上通知适配和自定义通知声音

在最近的项目开发中遇到了一个问题,8.0以上的通知无法正常显示和自定义的通知提示音无效的问题,今天特此记录一下,希望能够避免大家踩坑

由于Android 8.0 系统,Google为了提高用户体验,方便用户管理通知信息,同时也提高了通知到达率引入通知渠道。导致之前正常的写法无法直接使用

首先在Application中创建NotificationManager统一管理通知

由于NotificationChannel一旦被创建后无法修改 如果需要自定义提示音 需要提前吧所需的提示音配置完毕 然后指定通知渠道 发送时根据通知渠道即可发送

代码如下:

public class app extends Application {
    public static NotificationManager notificationChannelManager;

    public app() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationChannelManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            Uri huifang = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.huifang);
            Uri new_client = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.new_client);
            Uri new_dynamic = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.new_dynamic);
            Uri new_task = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.new_task);
            NotificationChannel huifangcannel = new NotificationChannel("huifang", "回访提示音", NotificationManager.IMPORTANCE_HIGH);
            NotificationChannel new_clientcannel = new NotificationChannel("new_client", "新客户提示音", NotificationManager.IMPORTANCE_HIGH);
            NotificationChannel new_dynamiccannel = new NotificationChannel("new_dynamic", "新动态提示音", NotificationManager.IMPORTANCE_HIGH);
            NotificationChannel new_taskcannel = new NotificationChannel("new_task", "新任务提示音", NotificationManager.IMPORTANCE_HIGH);
            huifangcannel.setSound(huifang, att);
            new_clientcannel.setSound(new_client, att);
            new_dynamiccannel.setSound(new_dynamic, att);
            new_taskcannel.setSound(new_task, att);
            notificationChannelManager.createNotificationChannel(huifangcannel);
            notificationChannelManager.createNotificationChannel(new_clientcannel);
            notificationChannelManager.createNotificationChannel(new_dynamiccannel);
            notificationChannelManager.createNotificationChannel(new_taskcannel);
        }
    }
}

发送通知

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    showNotification("huifang");
                } else {
                    showTongzhi();
                }

首先判断是API是否高于8.0 如果高于则使用通知渠道来发送通知 否则则使用普通通知

完整代码如下

    private void showTongzhi() {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(MainActivity.this,"")
                .setContentTitle("这是测试通知标题")  //设置标题
                .setContentText("这是测试通知内容") //设置内容
                .setWhen(System.currentTimeMillis())  //设置时间
                .setSmallIcon(R.mipmap.ic_launcher)  //设置小图标  只能使用alpha图层的图片进行设置
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))   //设置大图标
                .setAutoCancel(true)
                .setContentIntent(pi)
                .build();
        app.notificationChannelManager.notify(index, notification);
        index++;
    }


    /*
     * 简单的发送通知
     */
    @SuppressLint("NewApi")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showNotification(String qudaoid) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, qudaoid);
        builder
                .setContentTitle("title1111"+qudaoid)
                .setContentText("content1111"+qudaoid)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pi)
                .setAutoCancel(true);
        Notification notification = builder.build();
        app.notificationChannelManager.notify(index, notification);
        index++;

    }

demo链接地址https://gitee.com/AndroidLMY/TongZhiDemo

你可能感兴趣的:(Android8.0及以上通知适配和自定义通知声音)