Android通知Notification 兼容安卓8.0和8.0以下版本實現

重新拾起安卓,真是感受到了技術的日新月異。或許你一年不做,就已經是翻天覆地的變化。鬼知道爲什麼會這樣。。。

安卓通知算是個使用很頻繁的功能了把。最近在搗鼓這個,搗鼓了一天。

安卓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 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章