Android Notification通知欄的必備姿勢

Dusan_杜小菜發表於2016-05-18

android開發,可能經常使用到通知管理者,在手機頂部顯示通知。封裝一些常用的方法,可以減少程式碼冗餘。通常情況下,顯示下載進度,播放器通知欄操作,顯示APP狀態資訊,傳送友情提示等,都需要用到Notification。

一,直接貼程式碼,直觀瞭解通知欄。

/**
 * Created by duqian on 16/01/23.
 * Android通知欄封裝
 */
public class NotificationUtils {

    private static final int SmallIcon = R.drawable.ic_launcher;
    public static final int NotificationNumber = 1;
    private static NotificationManager mManager;
    private static NotificationCompat.Builder mBuilder;
    private static final Random RANDOM = new Random();
    /**
     * 獲取Builder
     */
    public static NotificationCompat.Builder getBuilder(Context context) {
        mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setNumber(NotificationNumber)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setAutoCancel(true);
              //.setDefaults(Notification.DEFAULT_VIBRATE);
        return mBuilder;
    }

    /**
     * 獲取NotificationManager
     */
    public static NotificationManager getManager(Context context) {

        if (mManager == null) {
            synchronized (NotificationUtils.class) {
                if (mManager == null) {
                    mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
                }
            }
        }
        return mManager;
    }


    /**
     * 顯示普通的通知
     */
    public static void showOrdinaryNotification(Context context, String title, String text, String ticker,
                                                int icon, int channel) {
        mBuilder = getBuilder(context);
        mManager = getManager(context);
        mBuilder.setContentTitle(title)
                .setContentText(text)
                .setContentIntent(getDefalutIntent(context, Notification.FLAG_AUTO_CANCEL))
                .setNumber(NotificationNumber)//顯示數量
                .setTicker(ticker)//通知首次出現在通知欄,帶上升動畫效果的,可設定文字,圖示
                .setWhen(System.currentTimeMillis())//通知產生的時間
                .setPriority(Notification.PRIORITY_DEFAULT)//設定該通知優先順序
                .setAutoCancel(true)//設定讓通知將自動取消
                .setOngoing(false)//ture,設定他為一個正在進行的通知。如一個檔案下載,網路連線。
                .setDefaults(Notification.DEFAULT_SOUND)//向通知新增聲音、閃燈和振動效果。最簡單、最一致的方式是使用當前的使用者預設設定,使用defaults屬性,可以組合:
                        //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 新增聲音 //DEFAULT_VIBRATE requires VIBRATE permission
                .setSmallIcon(SmallIcon)
        ;
        Notification mNotification = mBuilder.build();
        mNotification.icon = icon;
        mManager.notify(dealWithId(channel), mNotification);
    }
    //獲取預設的延期意圖
    public static PendingIntent getDefalutIntent(Context context, int flags) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, new Intent(), flags);
        return pendingIntent;
    }
    //通知channel ID,唯一標示一個通知
    public static int dealWithId(int channel) {
        return channel >= 1 && channel <= 100 ? channel : RANDOM.nextInt(Integer.MAX_VALUE - 100) + 101;
    }
    //獲取系統SDK版本
    public static int getSystemVersion() {
        int version = android.os.Build.VERSION.SDK_INT;
        return version;
    }

    /**
     * 清除所有的通知
     * @param context
     */
    public static void clearAllNotifification(Context context) {
        mManager = getManager(context);
        mManager.cancelAll();
    }

    /**
     * 清除通知
     */
    public static void clearNotifificationById(Context context, int channel){
        mManager = getManager(context);
        mManager.cancel(dealWithId(channel));
    }
    private static int iconId = 0;

    public void setIconId(int iconId){
        this.iconId=iconId;
    }
    /**
     * 預設圖示
     */
    private static int getPushIconId(Context context) {
        if(iconId==0)
        {
            iconId = context.getApplicationInfo().icon;
        }
        if (iconId < 0) {
            iconId = android.R.drawable.sym_def_app_icon;
        }
        return iconId;
    }
    //去通知文字的前15位字元
    private  static String getSubStringTitle(String desc) {
        if (desc != null && !"".equals(desc) && desc.length() > 15) {
            return desc.substring(0, 15);
        } else {
            return desc;
        }
    }
 }
/**
     * 顯示進度的通知欄
     */
    public static void showProgressNotification(Context context, String title, String text, String ticker,
                                                Intent resultIntent, boolean indeterminate, int progress,
                                                int icon, int channel) {
        mBuilder = getBuilder(context);
        mManager = getManager(context);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, RequestCode, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentTitle(ticker)//ticker,title
                .setContentText(text)
                .setTicker(ticker)
                .setSmallIcon(SmallIcon)
                .setContentIntent(pendingIntent);
        if (indeterminate) {
            //不確定進度的 設定為true就是不確定的那種進度條效果
            mBuilder.setProgress(0, 0, true).setOngoing(false).setAutoCancel(true);
        } else {
            //確定進度的
            mBuilder.setProgress(100, progress, false).setOngoing(true);
        }

        Notification progressNotification = mBuilder.build();
        progressNotification.icon = icon;
        mManager.notify(dealWithId(channel), progressNotification);
    }
 /**
     * 帶意圖的通知欄:Intent 中可以包含很多引數、功能
     * 應用場景:頁面啟動、跳轉、安裝apk
     */
    public static void showIntentNotification(Context context, String title, String text, String ticker,
                                              Intent resultIntent, int icon, int channel,int defaults) {
        mBuilder = getBuilder(context);
        mManager = getManager(context);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, RequestCode, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentTitle(title)
                .setContentText(text)
                .setTicker(ticker)
                .setSmallIcon(SmallIcon)
                .setContentIntent(pendingIntent);
        if (defaults>0){
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }
        Notification mNotification = mBuilder.build();
        mNotification.icon = icon;
        mManager.notify(dealWithId(channel), mNotification);

    }

二,如何呼叫呢?

    //提示儲存空間不足,跳轉到某個頁面
    private void notifyNotEnoughSpace(Context context) {
        String title = context.getString(R.string.error_sdcard_storage_full);
        String text = context.getString(R.string.buy_oair_device);
        String ticker = title;

        Intent resultIntent = new Intent(context,SimpleWebActivity.class);
        resultIntent.putExtra(MyConstant.intent_plugin_url,OairApi.oair_buy_product);
        resultIntent.putExtra(MyConstant.intent_plugin_name, context.getString(R.string.webview_oair_title));
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        NotificationUtils.showIntentNotification(context, title,text, ticker, resultIntent, R.drawable.ic_launcher, channel_download_ocloud,Notification.DEFAULT_ALL);//Notification.DEFAULT_SOUND
        //NotificationUtils.showOrdinaryNotification(context, title,text, ticker,R.drawable.logo_statusbar,channel_download_ocloud);
        //ToastUtils.showCenterToast(context,title+","+text);
    }

三,自定義通知View

以上通知欄,圖示,位置隨手機系統規格來。我們也可以自定義通知欄的佈局。通過引入xml中定義好的佈局,然後更新通知欄的UI。

//顯示應用有更新的通知,顯示下載進度,下載完成點選後可以安裝或其他操作
    private void showNotifycation(Context context) {
        String title = context.getString(R.string.notify_download_apk);
        String text = context.getString(R.string.notify_click_apk_download);
        String ticker = title;
        Notification n = new Notification(R.drawable.ic_launcher,ticker, System.currentTimeMillis());
        Intent clickIntent = new Intent();
        clickIntent.setAction(BroadcastUtils.ACTION_NOTIFICATION);
        clickIntent.putExtra(BroadcastUtils.CATEGORY,BroadcastUtils.category_apk_download_install);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        n.setLatestEventInfo(context, title, text, pendingIntent);
        n.flags |= Notification.FLAG_AUTO_CANCEL; //點選一次後自動消除
        final int hours = new Date().getHours();
        LogUtils.debug(TAG,"hours="+hours);
        if (hours>=8&&hours<=24) {
            n.defaults = Notification.DEFAULT_ALL;
        }
        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(channel_download_apk, n);
    }

通過程式碼可以看出,Notification的使用姿勢還是比較簡單的,而且通用,暫時沒有相容性問題。我自己用的挺好,是否優雅,自己驗證哈。
未完待續,Dusan,Q291902259,歡迎交流。

相關文章