com.bobomee.android.common.util.NotificationUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.bobomee.android.common.util.NotificationUtil.java

Source

/*
 * Copyright (C) 2016.  BoBoMEe(wbwjx115@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.bobomee.android.common.util;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;

public class NotificationUtil {

    /**
     * ??
     */
    public static NotificationManagerCompat initService(Context mContext) {
        NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(mContext);
        return mNotificationManager;
    }

    public static NotificationCompat.Builder invalidate(NotificationManagerCompat mNotificationManager,
            NotificationCompat.Builder mBuilder, int id) {
        mNotificationManager.notify(id, mBuilder.build());
        return mBuilder;
    }

    /**
     * ??
     */
    public static void clearNotify(NotificationManagerCompat mNotificationManager, int notifyId) {
        mNotificationManager.cancel(notifyId);// ID
    }

    /**
     * ?
     */
    public static void clearAllNotify(NotificationManagerCompat mNotificationManager) {
        mNotificationManager.cancelAll();// ?
    }

    /**
     * @?pendingIntent,2.3?
     * @flags: :Notification.FLAG_ONGOING_EVENT
     * Notification.FLAG_AUTO_CANCEL
     */
    private static PendingIntent getDefalutIntent(Context context, int flags) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, new Intent(), flags);
        return pendingIntent;
    }

    /**
     * ??
     */
    public static NotificationCompat.Builder initBuilder(Context context) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setWhen(System.currentTimeMillis())// ?
                .setContentIntent(getDefalutIntent(context, 0)).setPriority(Notification.PRIORITY_DEFAULT)// 
                .setOngoing(false)// ture
        // ???,??(?)???,?(,??,)
        // .setNumber(number)//?
        // .setAutoCancel(true)//?????
        // .setDefaults(Notification.DEFAULT_VIBRATE)//requires VIBRATE permission
        // Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 
        // ???????defaults.
        ;
        return mBuilder;
    }

    public static NotificationCompat.Builder setNotify(NotificationCompat.Builder mBuilder, String title,
            int iconId, String content, String ticker) {
        setContentTitle(mBuilder, title);
        setSmallIcon(mBuilder, iconId);
        setContentText(mBuilder, content);
        setTicker(mBuilder, ticker);
        return mBuilder;
    }

    public static NotificationCompat.Builder setContentTitle(NotificationCompat.Builder mBuilder, String title) {
        if (!TextUtils.isEmpty(title))
            mBuilder.setContentTitle(title);
        return mBuilder;
    }

    public static NotificationCompat.Builder setSmallIcon(NotificationCompat.Builder mBuilder, int iconId) {
        //??iconnotification ?
        mBuilder.setSmallIcon(iconId);
        return mBuilder;
    }

    public static NotificationCompat.Builder setContentText(NotificationCompat.Builder mBuilder, String content) {
        if (!TextUtils.isEmpty(content))
            mBuilder.setContentText(content);
        return mBuilder;
    }

    public static NotificationCompat.Builder setTicker(NotificationCompat.Builder mBuilder, String ticker) {
        if (!TextUtils.isEmpty(ticker))
            mBuilder.setTicker(ticker);
        return mBuilder;
    }

    public static NotificationCompat.Builder setActivityIntent(NotificationCompat.Builder mBuilder, Context context,
            Intent intent) {
        // ?ACTIONIntent
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        return mBuilder;
    }

    public static NotificationCompat.Builder setBroadCastIntent(NotificationCompat.Builder mBuilder,
            Context context, Intent intent) {
        // ?ACTIONIntent
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        return mBuilder;
    }
}