Android横幅通知栏自定义



        之前项目一直使用极光推送,极光推送自带通知提示,由于项目需求,
    极光的推送提示不能满足要求,所以只能自己完成推送提示操作。


    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.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Build;
    import android.support.v4.app.NotificationCompat;
    
    import com.leiren.airport.plat.R;
    import com.leiren.airport.plat.activity.HomeActivity;
    
    import java.util.Date;
    
    public class CustomNotification {

    //自定义通知对象
    private static CustomNotification customNotification = null;

    //通知管理器
    private static NotificationManager notificationManager = null;

    //通知ID
    private static int notifiId = 0;

    //通知标签
    private static String notifiTag = "";
    static {
        customNotification = new CustomNotification();
    }

    private CustomNotification(){
        super();
    }

    public static CustomNotification getInstance(){
        return customNotification == null? new CustomNotification():customNotification;
    }


    public void builderNotification(Context context,String sender,String title, String content, String extra){
        //此处判断安卓版本号是否大于或者等于Android8.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //通知栏提示
            notification_8(context,sender,title,content,extra);
        } else {
            //通知栏提示
            commonNotification(context,sender,title,content,extra);
        }
    }

    /**
     * Android5.0-8.0以下
     * @param context 上下文对象
     * @param sender    一般为app名称
     * @param title     推送标题
     * @param content   推送内容
     */
    @TargetApi(21)
    private void commonNotification(Context context,String sender,String title, String content, String extra){
        //获取通知管理器,用于发送通知
        notificationManager = notificationManager == null?
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE):notificationManager;
        //发送通知
        sendNotification(context,sender,title,content,extra);
    }

    /**
     * android8.0以上
     * @param context
     * @param sender
     * @param title
     * @param content
     * @param extra
     */
    @TargetApi(Build.VERSION_CODES.O)
    public void notification_8(Context context,String sender,String title, String content, String extra){
        //创建通道对象
        NotificationChannel channel = new NotificationChannel("com.leiren.airplat", "其他", NotificationManager.IMPORTANCE_HIGH);
        notificationManager = notificationManager == null?
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE):notificationManager;
        //设置通道
        notificationManager.createNotificationChannel(channel);
        //发送通知
        sendNotification(context,sender,title,content,extra);
    }



    private void sendNotification
    (Context context, String sender, String title,String content,String extra){
        // 创建一个Notification对象
        final NotificationCompat.Builder notification =
                 new NotificationCompat.Builder(context);
        // 设置打开该通知,该通知自动消失
        notification.setAutoCancel(true);
        // 设置显示在状态栏的通知提示信息
        notification.setTicker(sender);
        // 设置通知的小图标
        notification.setSmallIcon(R.drawable.ic_launcher);
        //设置下拉列表中的大图标
        notification.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher));
        // 设置通知内容的标题
        notification.setContentTitle(title);
        // 设置通知内容
        notification.setContentText(content);
        //设置发送时间
        notification.setWhen(System.currentTimeMillis());
        notification.setPriority(Notification.PRIORITY_MAX);//设置通知的优先级
        //向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
        notification.setDefaults(Notification.DEFAULT_ALL);

        //设置通知的等级
        // VISIBILITY_PUBLIC: 任何情况的显示
        //VISIBILITY_PRIVATE: 只有在没有锁屏时显示
        //VISIBILITY_SECRET: 在安全锁下或者没锁屏下显示
        notification.setVisibility(Notification.VISIBILITY_PUBLIC);

        // 关联PendingIntent 设置之后会一直显示悬浮通知不会消失
        //notification.setFullScreenIntent(pIntent1, true);

        // 创建一个启动其他Activity的Intent
        Intent intent = new Intent("cn.jpush.android.intent.NOTIFICATION_OPENED");
        intent.putExtra("cn.jpush.android.EXTRA",extra);
        PendingIntent pIntent = PendingIntent.getBroadcast(context, 1, intent, 0);


        Intent intent1 = new Intent(context, HomeActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //设置通知栏点击跳转
        notification.setContentIntent(pIntent);

        //发送通知
        notifiId = (int) new Date().getTime();//通知Id
        notifiTag = notifiId+"leiren";//通知标签
        notificationManager.notify(notifiTag,notifiId, notification.build());
        }
    }

遇见的问题:
    
    在写完之后发现android9.0一直没有横幅提示,而在android7.0上边横幅提示一直不能自动消失,百度了很多资料,
    实现横幅提示有两种方法:
    
    1、设置通知的优先级
    2、setFullScreenIntent方法

    我把两个都设置了,结果出现了上述问题,最后我把setFullScreenIntent方法注释了,android9.0的横幅提示就出现了,
    同时android7.0的横幅提示也会自动消失。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章