com.jhone.demo.utils.NotificationUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.jhone.demo.utils.NotificationUtils.java

Source

/*
 * Copyright  YOLANDA. All Rights Reserved
 *
 * 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.jhone.demo.utils;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.jhone.demo.common.AppApplication;

/**
 * Created by YOLANDA on 2016/3/12.
 */
public class NotificationUtils {

    /**
     * Android?
     */
    private NotificationManager mNotifyManager;

    /**
     * 
     *
     * @param id              ID
     * @param activityIntent  Activity
     * @param broadcastIntent Broadcast
     * @param stateBar        ?
     * @param title           
     * @param content         
     * @param iconResId       
     * @param progress        ?
     * @return {@link NotificationCompat.Builder}
     */
    public NotificationCompat.Builder createNotification(int id, Intent activityIntent, Intent broadcastIntent,
            CharSequence stateBar, CharSequence title, CharSequence content, int iconResId, int progress) {
        Context context = AppApplication.getInstance();
        mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(iconResId);
        builder.setTicker(stateBar);// ????
        builder.setContentTitle(title);// 
        builder.setContentText(content);// 
        builder.setOngoing(false);
        builder.setAutoCancel(false);
        builder.setProgress(100, progress, false);
        // 
        PendingIntent pendIntent;
        if (activityIntent != null) {
            pendIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
        } else {
            pendIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, 0);
        }
        builder.setContentIntent(pendIntent); // ?PendingIntent
        mNotifyManager.notify(id, builder.build());
        return builder;
    }

    /**
     * 
     *
     * @param id
     * @param builder
     * @param content
     * @param progress
     */
    public void update(int id, NotificationCompat.Builder builder, CharSequence content, int progress) {
        builder.setContentText(content).setProgress(100, progress, false);
        builder.setAutoCancel(false).setOngoing(true);
        mNotifyManager.notify(id, builder.build());
    }

    /**
     * ?
     *
     * @param id
     * @param builder
     * @param title
     * @param content
     */
    public NotificationCompat.Builder finish(int id, NotificationCompat.Builder builder, CharSequence title,
            CharSequence content, Intent intent, boolean isLong) {
        mNotifyManager.cancel(id);
        builder.setContentText(content).setProgress(100, 100, false);
        builder.setAutoCancel(false).setOngoing(isLong);
        PendingIntent pendIntent = PendingIntent.getActivity(AppApplication.getInstance(), 0, intent, 0);
        builder.setContentIntent(pendIntent);
        mNotifyManager.notify(id, builder.build());
        return builder;
    }

    /**
     * ?
     *
     * @author YOLANDA
     */
    public void cancel(int id) {
        mNotifyManager.cancel(id);
    }

}