Notification 兼容android8.0 自带震动和音效的通知栏方案

import android.annotation.TargetApi;

import android.app.Notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.media.AudioAttributes;

import android.media.AudioManager;

import android.net.Uri;

import android.os.Build;

import android.provider.Settings;

import android.support.v4.app.NotificationCompat;

import com.bondex.ysl.bondex.R;

import com.bondex.ysl.bondex.main.MainActivity;

import com.orhanobut.logger.Logger;

import me.goldze.mvvmhabit.utils.constant.Constant;

import static android.content.Context.NOTIFICATION_SERVICE;

/**

* date: 2019/4/23

* Author: ysl

* description:

*/

public class NotificationUtils {

private Contextcontext;

    private NotificationCompat.Builderbuilder;

    private NotificationManagernotificationManager;

    private PendingIntentpendingIntent;

    public NotificationUtils(Context context) {

this.context = context;

        notificationManager = (NotificationManager) context.getSystemService(

NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

String channelName ="聊天消息";

            int importance = NotificationManager.IMPORTANCE_HIGH;

            createNotificationChannel(Constant.CHANNELID, channelName, importance);

        }

//即将要发生的意图,他可以被取消和更新

        Intent intent =new Intent(context, MainActivity.class);

        intent.putExtra(Constant.NOTIFICATION_KEY, Constant.NOTIFICATION_VALUE);

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

        builder =new NotificationCompat.Builder(context, Constant.CHANNELID);

    }

@TargetApi(Build.VERSION_CODES.O)

private void createNotificationChannel(String channelId, String channelName, int importance) {

NotificationChannel channel =new NotificationChannel(channelId, channelName, importance);

        channel.setBypassDnd(true);    //设置绕过免打扰模式

        channel.canBypassDnd();      //检测是否绕过免打扰模式

        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知

        channel.setSound(resourceIdToUri(context, R.raw.reminder),

                new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)

.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)

.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT).build());

        channel.setShowBadge(true);

        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});//设置震动频率

        channel.enableVibration(true);

        //设置可绕过  请勿打扰模式

        channel.setBypassDnd(true);

        notificationManager.createNotificationChannel(channel);

    }

public void sendMsg(String content) {

Notification notification =builder

                .setContentTitle("收到一条新消息")

.setContentText(content)

.setContentIntent(pendingIntent)

.setSmallIcon(R.mipmap.ic_launcher)

.setNumber(3)

//                .setVibrate(new long[]{100,200,300,400,500,600})

//                //设置通知时间

                .setWhen(System.currentTimeMillis())

.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)

.setAutoCancel(true)

.build();

        notificationManager.notify(1, notification);

    }

public NotificationgetNotification(String title, String content) {

return builder

                .setContentIntent(pendingIntent)

.setSmallIcon(R.mipmap.ic_launcher)

.setNumber(3)

.setContentTitle(title)

.setContentText(content)


//                //设置通知时间

                .setWhen(System.currentTimeMillis())

.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)

.setAutoCancel(true)

.build();

    }

private final StringANDROID_RESOURCE ="android.resource://";

    private final StringFOREWARD_SLASH ="/";

    private UriresourceIdToUri(Context context, int resourceId) {

return Uri.parse(ANDROID_RESOURCE + context.getPackageName() +FOREWARD_SLASH + resourceId);

    }

}

你可能感兴趣的:(Notification 兼容android8.0 自带震动和音效的通知栏方案)