NotificationUtils.java 8.64 KB
package com.lotus.town.notify;

import android.annotation.TargetApi;
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.media.AudioManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;


import com.lotus.town.R;
import com.lotus.town.ali.MonitorService;

import static android.content.Context.NOTIFICATION_SERVICE;
public class NotificationUtils {
    private static NotificationUtils notificationUtils;

    public static NotificationUtils init() {
        if (notificationUtils == null) {
            notificationUtils = new NotificationUtils();
        }
        return notificationUtils;
    }

    /**
     * 适配8.0通知栏
     * 在启动页或首页中创建通知渠道
     */
    public void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //通知渠道
            String channelId = "systemmsg";
            String channelName = "系统消息";
            int importance = NotificationManager.IMPORTANCE_LOW;
            setNotificationChannel(context, channelId, channelName, importance);
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void setNotificationChannel(Context context, String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        //设置不显示角标
        channel.setShowBadge(false);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }


    public Notification sendYmNotify(Context context, String ticket,String title) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_view);
        RemoteViews smallview = new RemoteViews(context.getPackageName(), R.layout.notification_view);

        view.setTextViewText(R.id.notification_title, title);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //8.0及以上,需传入渠道的id
            Notification notification = new NotificationCompat.Builder(context, "systemmsg")
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setCustomBigContentView(view)

                    .setCustomContentView(smallview)
                    .setTicker(ticket)
                    .setWhen(System.currentTimeMillis())
                    .build();
            notification = setAlarmParams(context, notification, "", "");
            notification.flags = Notification.DEFAULT_ALL;
            notification.contentIntent = PendingIntent.getActivity(context, 0,
                    getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
            manager.notify(111, notification);
            return notification;
        } else {
            Notification notification = new Notification.Builder(context)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContent(view)
                    .setTicker(ticket)
                    .setWhen(System.currentTimeMillis())
                    .build();
            notification.flags = Notification.DEFAULT_ALL;
            notification = setAlarmParams(context, notification, "", "");
            notification.contentIntent = PendingIntent.getActivity(context, 0,
                    getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
            manager.notify(111, notification);
            return notification;
        }
    }


    /**
     * 根据消息type自定义通知动作
     *
     * @return
     */
    public static Intent getIntent(Context context,Class clz) {
        Intent intent = new Intent();
        intent.setClass(context, clz);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        return intent;
    }

    public static Intent getIntent(Context context,String name) {
        Intent intent = new Intent();
        intent.setClassName(context, name);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        return intent;
    }

    /**
     * 新消息通知模式
     *
     * @param context
     * @param notification
     * @return
     */
    private Notification setAlarmParams(Context context, Notification notification, String sound, String vibrate) {
        AudioManager volMgr = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        switch (volMgr.getRingerMode()) {// 获取系统设置的铃声模式
            case AudioManager.RINGER_MODE_SILENT:// 静音模式,值为0,这时候不震动,不响铃
                notification.sound = null;
                notification.vibrate = null;
                break;
            case AudioManager.RINGER_MODE_VIBRATE:// 震动模式,值为1,这时候震动,不响铃
                if (vibrate.equals("1")) {
                    notification.defaults |= Notification.DEFAULT_VIBRATE;
                    notification.sound = null;
                } else {
                    notification.sound = null;
                    notification.vibrate = null;
                }
                break;
            case AudioManager.RINGER_MODE_NORMAL:// 常规模式,值为2,分两种情况:1_响铃但不震动,2_响铃+震动
                if (sound.equals("1") && vibrate.equals("1")) {// 声音+震动
                    notification.defaults |= Notification.DEFAULT_VIBRATE;
                    notification.defaults |= Notification.DEFAULT_SOUND;
                } else if (sound.equals("0") && vibrate.equals("1")) {// 震动 ,不响铃
                    notification.defaults |= Notification.DEFAULT_VIBRATE;
                    notification.sound = null;
                } else if (sound.equals("1") && vibrate.equals("0")) {// 响铃,不震动
                    notification.defaults |= Notification.DEFAULT_SOUND;
                    notification.vibrate = null;
                } else {// 静音
                    notification.sound = null;
                    notification.vibrate = null;
                }
                break;
            default:
                break;
        }
        return notification;
    }

    public Notification sendNotify(Context context, PushModel model, String sound, String vibrate) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //8.0及以上,需传入渠道的id
            Notification notification = new NotificationCompat.Builder(context, "systemmsg")
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.empty)
                    .setContentTitle("荷花小镇")
                    .setContentText("小青蛙为您持续赚荷花中")
                    .setSubText("小青蛙为您持续赚荷花中")
                    .setWhen(System.currentTimeMillis())
                    .build();
            notification = setAlarmParams(context, notification, sound, vibrate);
            notification.flags = model.getFlag();
            notification.contentIntent = PendingIntent.getActivity(context, 0,
                    getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
            manager.notify(111, notification);
            return notification;
        } else {
            Notification notification = new Notification.Builder(context)
                    .setAutoCancel(true)
                    .setContentTitle("荷花小镇")
                    .setContentText("小青蛙为您持续赚荷花中")
                    .setSubText("小青蛙为您持续赚荷花中")
                    .setSmallIcon(R.drawable.empty)
                    .setWhen(System.currentTimeMillis())
                    .build();
            notification.flags = model.getFlag();
            notification.contentIntent = PendingIntent.getActivity(context, 0,
                    getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
            notification = setAlarmParams(context, notification, sound, vibrate);
            manager.notify(111, notification);
            return notification;
        }
    }
}