Notification之8.0以上无效解决

  • 今天使用Notification做一个通知消息的时候,突然TMD居然无效,反复的搞整无果,于是乎谷歌了一把,发现跟系统版本有关系,貌似8.0系统以上的系统使用以前的方法实现都无效,万能的百度告诉我需要设置渠道,结果可以正常实现了,废话不多说了,直接上马:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import com.jbh.goldrush.R;
import static android.content.Context.NOTIFICATION_SERVICE;

public class NotificationUtil {


    private Context mContext;
    private NotificationManager notificationManager;
    private int progress = 0 ;
    private int notificationId = 0x2154;
    private Notification.Builder builder;
    private String ContentTitle = "";
    private String ContentText = "";


    /**
     * @param context
     */
    public NotificationUtil(Context context){
        this.mContext = context;
        notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        sendNotification();
    }


    @TargetApi(Build.VERSION_CODES.O)
    private void sendNotification(){
        String channelId = "my_channel_01";
        String name="我是渠道名字";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel mChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(mChannel);
            builder = new Notification.Builder(mContext)
                    .setChannelId(channelId)  //设置渠道id
                    .setContentTitle(ContentTitle)
                    .setContentText(ContentText)
                    .setSmallIcon(R.mipmap.app_logo);
            LogUtils.TOAST("O");
        } else {
            builder = new Notification.Builder(mContext)
                    .setContentTitle(ContentTitle)
                    .setContentText(ContentText)
                    .setSmallIcon(R.mipmap.app_logo)
                    .setOngoing(true)
                    .setChannelId(channelId);//无效
            LogUtils.TOAST("dfasd");
        }
        notificationManager.notify(notificationId, builder.build());
    }


    /**
     * @param contentText
     * @return
     */
    public NotificationUtil setContentText(String contentText) {
        ContentText = contentText;
        return NotificationUtil.this ;
    }


    /**
     *
     * @param contentTitle
     * @return
     */
    public NotificationUtil setContentTitle(String contentTitle) {
        ContentTitle = contentTitle;
        return NotificationUtil.this ;
    }




    /**
     * @param progress
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setProgress(int progress){
        if (builder==null)return;
        builder.setProgress(100,progress,false);
        builder.setContentTitle("下载中...");
        if (progress==100){
            builder.setContentTitle("下载完成");
            //自动移除
            notificationManager.cancel(notificationId);
        }else {
            notificationManager.notify(notificationId, builder.build());//显示通知
        }
    }



   public void ceShi(){
    new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void run() {
                while (progress++<100){
                    setProgress(progress);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

将哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值