android 8.0 notification通知适配踩坑

老项目通知这块要适配8.0, 所以按照老套路 打开百度 深入andoid notificion 8.0 ctrl c and ctrl+v 一顿熟悉的操作, 然后测试运行项目 ! 尼玛, 不行!! 在8.0上不行 ,心里一顿的问候, 然后开始网上各种解决办法对比,自己的代码跟别人说的一样啊 加一个channel就可以了啊 为什么就是不行呢, 然后对比官网的api做法也是一样啊, 查了好久就是没有找到原因,尼玛 心态崩溃有木有! 对于我这种自卑心态的人让我一度怀疑人生! 

心里想不行! 不能被这点问题打败,收拾下心情,再搜下官方demo吧,把demo拿来用用  ,于是拿来demo 一运行竟然可以,! 代码什么的 我写的和官网一样啊 为什么我的不行 没天理啊 有木有! 以下是官网代码: 

class NotificationHelper extends ContextWrapper {
    private NotificationManager manager;
    public static final String PRIMARY_CHANNEL = "default";
    public static final String SECONDARY_CHANNEL = "second";
    @RequiresApi(api = Build.VERSION_CODES.O)
    public NotificationHelper(Context ctx) {
        super(ctx);
 
        NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
                "ff", NotificationManager.IMPORTANCE_DEFAULT);
        chan1.setLightColor(Color.GREEN);
        chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        getManager().createNotificationChannel(chan1);

    } 
    
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getNotification1(String title, String body) {
        return new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL)
                 .setContentTitle(title)
                 .setContentText(body)
                 .setSmallIcon(getSmallIcon())
                 .setAutoCancel(true);
    } 
    
    public Notification.Builder getNotification2(String title, String body) {
        return new Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL)
                 .setContentTitle(title)
                 .setContentText(body)
                 .setSmallIcon(getSmallIcon())
                 .setAutoCancel(true);
    } 
    
    public void notify(int id, Notification.Builder notification) {
        getManager().notify(id, notification.build());
    } 
    
    private int getSmallIcon() { 
        return android.R.drawable.stat_notify_chat;
    } 
    
    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        } 
        return manager;
    } 
} 

我的代码: 


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotifactionChannel();
        }
        Intent it = new Intent(this, TestNoticon1.class);
//        Notification.Builder notifaction = createNotifaction("fuck title", "this is the 
 fuck the message f", null);
        Notification.Builder notification1 = getNotification1("fuck", "dfadfasdfadsfadf");
        getNotiManger().notify(1, notification1.build());

  private void createNotifactionChannel() {
        NotificationChannel channel = new NotificationChannel("p9", "chanel2", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setLightColor(Color.RED);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        getNotiManger().createNotificationChannel(channel);
        Log.e("zyh", "------createChannel---");
    }


   @RequiresApi(api = Build.VERSION_CODES.O)
    private Notification.Builder createNotifaction(String title, String content, Intent it) {

        Notification.Builder builder = new Notification.Builder(getApplicationContext(), "fss")
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.stat_notify_chat)
                .setAutoCancel(true);
        if (it != null) {
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);
        }
        return builder;
    }

代码基本是复制的, 什么 “基本”!!  本着严谨的科学态度, 我决定一行代码一行代码的去对比,,,,

 经过对比发现  在new Notification.Build(string id, string channelid)时, 当channele为 “deafult" 和”second‘时 在8.0可以成功发布通知!!!  为什么为什么!!! 难道就只能用这两个固定了,这也太违背人道主义精神了吧!! 然后去看api文档, 但是官方文档都没有明确说明必须用这两个作为channel啊 !!! 问题出在哪里呢 :: 

 冷静一下, 然后想到 设置channel的时候,指定了一个id,这个id 和builder构造函数中的id, 需要一样,才可以!!!!

问题就出在这里, new NotificationChannel(channel_id,chanel_name,improtance)

     new Notification.Builder(context,channel_id)  这两个id 需要保持一致!!!!!  我tm真是笨啊!! Google文档也没说要保持一样啊!!!!  。。。。。。。。

Notification.Builder.(context,id)在8.0下 会报错, 要做兼容处理,或者使用NoticationCompat.Builer (context,id)处理兼容问题

猜你喜欢

转载自blog.csdn.net/qq_36174100/article/details/82686475