NotificationUtils 对于各个版本的通知栏工具类

package com.hexie.framework.util;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;


import com.hexie.framework.R;


import java.util.UUID;




/**
 * Created by daxiong on 2015/8/7.
 */
@SuppressWarnings("all")
public class NotificationUtils  {


    /**
     * 优选版本通知发送
     * @param context
     * @param intent
     * @param icon
     * @param title 设置通知栏标题
     * @param text
     * @param nunber 设置通知集合的数量
     * @param notification_flag
     */
    public static void send(Context context ,Intent intent,int icon,String title, String text ,int nunber,int notification_flag){
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        int sdkInt = Build.VERSION.SDK_INT;
        L.i("notification", "sdkInt:" + sdkInt);
        if (sdkInt >= 16){
            PendingIntent pendingIntent16 = PendingIntent.getActivity(context, UUID.randomUUID().hashCode(),
                    intent , 0);
            // 通过Notification.Builder来创建通知,注意API Level
            // API16之后才支持
            Notification notify16 = new Notification.Builder(context)
                    .setSmallIcon(icon)
                    .setTicker(text)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setPriority(Notification.PRIORITY_MAX) //设置该通知优先级
                    .setContentIntent(pendingIntent16)
                    .setNumber(nunber)
                    .build(); // 需要注意build()是在API
            // level16及之后增加的,API11可以使用getNotificatin()来替代
            notify16.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
            manager.notify(notification_flag, notify16);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示


        }else if (sdkInt < 16 && sdkInt >= 11){
            PendingIntent pendingIntent11 = PendingIntent.getActivity(context, UUID.randomUUID().hashCode(),
                    intent, 0);
            // 通过Notification.Builder来创建通知,注意API Level
            // API11之后才支持
            Notification notify11 = new Notification.Builder(context)
                    .setSmallIcon(icon) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
                            // icon)
                    .setTicker(text)// 设置在status
                            // bar上显示的提示文字
                    .setContentTitle(title)// 设置在下拉status
                            // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
                    .setContentText(text)// TextView中显示的详细内容
                    .setContentIntent(pendingIntent11) // 关联PendingIntent
                    .setNumber(nunber) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
                    .getNotification(); // 需要注意build()是在API level
            // 16及之后增加的,在API11中可以使用getNotificatin()来代替
            notify11.flags |= Notification.FLAG_AUTO_CANCEL;
            manager.notify(notification_flag, notify11);
        }else {//已废弃,不过兼容所有版本的通知
            // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。


            // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
            PendingIntent pendingIntent = PendingIntent.getActivity(context, UUID.randomUUID().hashCode(),
                    intent, 0);
            // 下面需兼容Android 2.x版本是的处理方式
            Notification notify1 = new Notification();
            notify1.icon = icon;
            notify1.tickerText = text;
            notify1.when = System.currentTimeMillis();
            notify1.setLatestEventInfo(context, title,
                    text, pendingIntent);
            notify1.number = nunber;
            notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
            // 通过通知管理器来发起通知。如果id不同,则每click,在statubar那里增加一个提示
            manager.notify(notification_flag, notify1);
        }




    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值