android 8.0通知栏的适配

public class NotificationHelper {
    private final String TAG = NotificationHelper.class.getSimpleName();
    private NotificationManager mNotificationManager;
    private Notification mNotification;
    private NotificationCompat.Builder mBuilder;
    private int mNotificationId = 0x1234;
    private NotificationChannel mNotificationChannel;


    public NotificationHelper() {

    }

    //初始化通知
    public void initNotification() {
        mNotificationManager = (NotificationManager) StarNewsSdk.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        String channelId = "1";
        if (Build.VERSION.SDK_INT >= 26) {
            if (mNotificationChannel == null) {
                String channelName = "xxxx";
                mNotificationChannel = new NotificationChannel(channelId,
                        channelName, NotificationManager.IMPORTANCE_HIGH);
                mNotificationChannel.enableLights(true);//是否在桌面icon右上角展示小红点
                mNotificationChannel.setLightColor(Color.GREEN);//小红点颜色
                mNotificationManager.createNotificationChannel(mNotificationChannel);
            }
        }

        Intent intent = new Intent(context, ContinueDownloadBroadcastReceiver.class);
        intent.putExtra("notificationId", mNotificationId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = new NotificationCompat.Builder(StarNewsSdk.getContext(), channelId);
        mBuilder.setOnlyAlertOnce(true);
        mBuilder.setContentTitle("下载好头条APP...") //设置通知标题
//                .setSmallIcon()//设置通知小图标
//                .setLargeIcon() //设置通知的大图标
                .setDefaults(Notification.DEFAULT_LIGHTS) //设置通知的提醒方式: 呼吸灯
                .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级:最大
                .setAutoCancel(false)//设置通知被点击一次是否自动取消
                .setContentIntent(pendingIntent)
                .setContentText("下载进度: 0%")
                .setProgress(100, 0, false);
        mNotification = mBuilder.build();
    }

    /**
     * 更新下载进度
     *
     * @param currentBytes 当前已下载大小
     * @param totalBytes   文件总大小
     */
    public void setProgress(long currentBytes, long totalBytes) {
        if (mBuilder == null || mNotificationManager == null) {
            return;
        }
        long progress = currentBytes * 100 / totalBytes;
        mBuilder.setProgress(100, (int) progress, false);
        mBuilder.setContentText("下载进度:" + (int) progress + "%");
        mNotification = mBuilder.build();
        mNotificationManager.notify(mNotificationId, mNotification);
        if (currentBytes == totalBytes) {
            Log.d(TAG, "下载完成取消通知栏");
            cancelNotification();
        }
    }

    public void cancelNotification() {
        if (mNotificationManager == null) {
            return;
        }
        mNotificationManager.cancel(mNotificationId);
    }
}

通知栏的点击事件可以响应直接打开一个activity或者开启service,如果只是普通点击事件则需要增加广播,如上面的Intent

public class ContinueDownloadBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //点击notification, 继续下载
        Intent downloadAppIntent = new Intent(ReceiverActionInterface.ACTION_DOWNLOAD_APP);
        LocalBroadcastManager.getInstance(context).sendBroadcast(downloadAppIntent);
    }
}

ContinueDownloadBroadcastReceiver 需要在AndroidManifest在声明,在广播接收者中处理点击逻辑

你可能感兴趣的:(android 8.0通知栏的适配)