Notication 应用

package union.com.viewgroup.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import union.com.viewgroup.R;
import union.com.viewgroup.ui.MainActivity;

import static androidx.core.app.NotificationCompat.BADGE_ICON_SMALL;


/*
 * @Author   xiaobo
 * @Date     2019/9/19  11:39
 * @Version  1.0
 * @Desc     通知栏管理类
 */
public class NotificationManagerUtils {

    /**
     * 通道Id
     */
    private static final String CHANNEL_ID = "channel.id";

    /**
     * 通知Id
     */
    private static final int NOTIFICATION_ID = 1001;

    /**
     * 点击通知需要跳转到的Activity
     */
    private Class<?> aClass = MainActivity.class;

    /**
     * 日志Tag
     */
    private static final String TAG = NotificationManagerUtils.class.getSimpleName();

    /**
     * 通知管理器
     */
    private NotificationManager manager;
    private static NotificationManagerUtils instance;
    private Notification notification;


    private NotificationManagerUtils() {

    }

    /**
     * 获取NotificationManagerUtils 实例
     * @return   NotificationManagerUtils
     */
    public static NotificationManagerUtils getInstance() {
        if (instance == null) {
            instance = new NotificationManagerUtils();
        }
        return instance;
    }

    /**
     * 获取通知管理器
     * @param context   上下文对象
     */
    public void getNotificationService(Context context) {
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 获取通知管理器
     * @return  NotificationManager
     */
    public NotificationManager getManager() {
        return manager;
    }

    /**
     * 创建通知
     * @param context   上下文对象
     */
    private void createNotification(Context context) {
        if(manager == null){
            getNotificationService(context);
        }
        NotificationCompat.Builder builder = buildNotificaiton(context);
         notification = builder.build();
        manager.notify(NOTIFICATION_ID, notification);
    }

    /**
     * 发送通知
     * @param context   上下文对象
     */
    public void sendNotification(Context context){
        if(notification == null){
            createNotification(context);
        }
        manager.notify(NOTIFICATION_ID, notification);
    }


    /**
     * 取消通知
     */
    public void cancelNotification() {
        manager.cancel(NOTIFICATION_ID);
    }

    /**
     * 创建通知builder
     * @param context 上下文对象
     * @return  NotificationCompat.Builder
     */
    private NotificationCompat.Builder buildNotificaiton(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        builder.setColor(context.getResources().getColor(R.color.colorAccent))
                .setContentIntent(createContentIntent(context))
                .setContentTitle("通知content")
                .setContentText("通知内容")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setBadgeIconType(BADGE_ICON_SMALL)
                .setWhen(System.currentTimeMillis())
                .setVibrate(new long[]{1000, 500, 2000});
        return builder;
    }

    /**
     * 创建通知channel
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createChannel() {
        if (manager.getNotificationChannel(CHANNEL_ID) == null) {
            CharSequence name = "media Session";
            String description = "description";
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.enableVibration(true);
            channel.setVibrationPattern(
                    new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            manager.createNotificationChannel(channel);
            Log.d(TAG, "createChannel: New channel created");
        } else {
            Log.d(TAG, "createChannel:Existing channel reused");
        }
    }


    private PendingIntent createContentIntent(Context context) {
        Intent openUI = new Intent(context, aClass);
        openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return PendingIntent.getActivity(
                context, 0, openUI, 0);
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值