Android通知Notification 兼容安卓8.0和8.0以下版本实现

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wjyyawjx/article/details/80020947

重新拾起安卓,真是感受到了技术的日新月异。或许你一年不做,就已经是翻天覆地的变化。鬼知道为什么会这样。。。

安卓通知算是个使用很频繁的功能了把。最近在捣鼓这个,捣鼓了一天。

安卓8.0对于Notification的创建,重新定义,需要一个channel的概念。没有去细究。

直接看实现代码,可以兼容7.0和8.0的---注意坑爹的就是8.0的代码在7.0模拟器运行失败。7.0代码在8.0模拟器运行也失败。

弱弱的说一句,说好的兼容呢?参照网上其他博文改编了一下。

package com.example.pengda.android_activity.services;

/**
 * Created by pengda on 2018/4/20.
 */

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.example.pengda.android_activity.R;


public class NotificationUtils extends ContextWrapper {

    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    public  Notification notification;
    public NotificationUtils(Context context){
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel(){
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }
    private NotificationManager getManager(){
        if (manager == null){
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(String title, String content){
        return new Notification.Builder(getApplicationContext(), id)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true);
    }
    public Notification.Builder getNotification_25(String title, String content){
//        return new NotificationCompat.Builder(getApplicationContext())
//                .setContentTitle(title)
//                .setContentText(content).
//        setSmallIcon( R.mipmap.ic_launcher).setLargeIcon( BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//                .setAutoCancel(true);

              return new Notification.Builder(this).setTicker("123").
                setSmallIcon(R.mipmap.ic_launcher).setLargeIcon( BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentText("123").setContentTitle( "ashdash" );

    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void sendNotification(String title, String content){
        if (Build.VERSION.SDK_INT>=26){
            createNotificationChannel();
            this.notification = getChannelNotification
                    (title, content).build();
            getManager().notify(1,notification);
        }else{
            this.notification = getNotification_25(title, content).build();
            getManager().notify(1,notification);
        }
    }



    public Notification getNotification(){

        return this.notification;
    }
}

这个工具类我参照了此博文 https://blog.csdn.net/z642385985/article/details/78583980?locationNum=9&fps=1 


展开阅读全文

没有更多推荐了,返回首页