Android 通知栏 Notification兼容版本Build.VERSION_CODES.O(26)

项目中遇到通知栏消息提示,版本更新的时候,为了兼容高版本,差了些资料,做了不少尝试,整理了一下,源码如下,还可以在优化:

转载请注明地址:

https://blog.csdn.net/sdbzmyh0/article/details/80756434

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.mxsoft.test.MainActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by abeils on 2018/6/15.
 */

public class NotificationUtilsMe/* extends ContextWrapper*/ {
    public static final String channelId = "channel_1";
    public static final String channelName = "channel_name_1";

    private Context context;

    private NotificationManager notificationManager;
    private NotificationCompat.Builder builder;
    private Notification.Builder builder26;
    private int notifyId=1;
    PendingIntent pendingIntent;
    public NotificationUtilsMe(Context context){
        //super(context);
        this.context=context;

        Intent intent=new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel(){
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true);
        channel.enableVibration(true);

        getManager().createNotificationChannel(channel);
    }
    private NotificationManager getManager(){
        if (notificationManager == null){
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notificationManager;
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(String title, String content){
        createNotificationChannel();
        builder26= new Notification.Builder(context.getApplicationContext(), channelId)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
        ;
        return builder26;
    }
    public NotificationCompat.Builder getNotification_25(String title, String content){
        builder= new NotificationCompat.Builder(context.getApplicationContext())
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
        ;
        return builder;
    }
    int time=0;
    public void sendNotification(final String title, String content){
        final Notification notification;
        if (Build.VERSION.SDK_INT>=26){
            //createNotificationChannel();
            notification = getChannelNotification(title, content).build();
        }else{
            notification = getNotification_25(title, content).build();
        }
        getManager().notify(notifyId,notification);

    }
    public void updateProgress(int progress){
        if(time<100) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                builder26.setProgress(100, time++, false);
                builder26.setContentInfo(time+"%");
                notificationManager.notify(notifyId,builder26.build());
            } else {
                builder.setProgress(100, time++, false);
                builder.setContentInfo(time+"%");
                notificationManager.notify(notifyId,builder.build());
            }
        }
        else {

        }
    }
}
 
  

应用的话,可以在Activity中这样用

notificationUtils = new NotificationUtilsMe(MainActivity.this);
notificationUtils.sendNotification("测试标题", "测试内容");
handler.sendEmptyMessageDelayed(1,100);
 
  

其中handler定义如下

Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        notificationUtils.updateProgress(progress++);
        handler.sendEmptyMessageDelayed(1,100);
    }
};
邮箱:[email protected],qq:279931514,微信:sdbzmyh

 
  

你可能感兴趣的:(Android 通知栏 Notification兼容版本Build.VERSION_CODES.O(26))