android 自定义通知栏

一、创建通知栏布局文件 view_notification_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation_list_item_layout"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/black"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:clickable="true"
    android:focusable="true"
    >

    <TextView
        android:id="@+id/btn_see"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/dimen_10"
        android:textSize="@dimen/text_size_large"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:textColor="@color/white"
        android:text="see" />

    <TextView
        android:id="@+id/btn_ignore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn_see"
        android:padding="@dimen/dimen_10"
        android:textSize="@dimen/text_size_large"
        android:textColor="@color/white"
        android:layout_marginRight="20dp"
        android:text="ignore" />
    <TextView
        android:id="@+id/view_separator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:background="@color/white"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:layout_centerVertical="true"
        android:text="@string/surprise_notification_msg"/>
</LinearLayout>
二、创建通知栏事件监听广播,并动态注册
NotificationReceiver.getInstance().registerReceiver();
广播实现如下:
public class NotificationReceiver extends BroadcastReceiver {
    private static final String TAG = AppConstants.APP_TAG + "NotificationReceiver ";
    private static volatile NotificationReceiver mInstance;
    //private static IIviStateChangeListener mIviStateChangeListener;//not static broadcast,set this to static
    private Handler mWorker;

    public NotificationReceiver() {
        mWorker = new Handler();
    }

    /**
     * get Instance.
     *
     * @return instance.
     */
    public static NotificationReceiver getInstance() {
        if (mInstance == null) {
            synchronized (NotificationReceiver.class) {
                if (mInstance == null) {
                    mInstance = new NotificationReceiver();
                }
            }
        }
        return mInstance;
    }

    /**
     * registerReceiver.
     */
    public void registerReceiver() {
        //LogUtil.d(TAG + "registerReceiver private");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AppConstants.NOTIFICATION_ACTION_SEE);
        intentFilter.addAction(AppConstants.NOTIFICATION_ACTION_IGNORE);
        MyApplication.getAppContext().registerReceiver(NotificationReceiver.getInstance(),
            intentFilter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtil.d(TAG + "onReceive 000");
        String action = null;
        if (intent != null) {
            action = intent.getAction();
            LogUtil.d(TAG + "onReceive action:" + action);
        }
        if (AppConstants.NOTIFICATION_ACTION_SEE.equals(action)) {
            Intent i = new Intent(context, MainActivity.class);
            context.startActivity(i);
        } else if (AppConstants.NOTIFICATION_ACTION_IGNORE.equals(action)) {
            NotificationManager notificationManager = (NotificationManager) MyApplication.getAppContext().getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(AppConstants.NOTIFICATION_ID);
        }
    }
}
三、调用通知并显示
NotificationUtil.getInstance().showNotification();
通知工具类如下:
public class NotificationUtil {
    private static final String TAG = AppConstants.APP_TAG + "NotificationUtil ";
    private static volatile NotificationUtil mInstance;
    private static Context mContext = MyApplication.getAppContext();

    public NotificationUtil() {
    }

    /**
     * get Instance.
     *
     * @return instance.
     */
    public static NotificationUtil getInstance() {
        if (mInstance == null) {
            synchronized (NotificationUtil.class) {
                if (mInstance == null) {
                    mInstance = new NotificationUtil();
                }
            }
        }
        return mInstance;
    }

    public void showNotification() {
        LogUtil.d(TAG + "testNotification*");
        RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.view_notification_layout);//自定义的布局视图

        //按钮点击事件:

        PendingIntent seeIntent = PendingIntent.getBroadcast(mContext, 1, new Intent(
            AppConstants.NOTIFICATION_ACTION_SEE), PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent ignoreIntent = PendingIntent.getBroadcast(mContext, 1, new Intent(
            AppConstants.NOTIFICATION_ACTION_IGNORE), PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btn_see, seeIntent);//
        views.setOnClickPendingIntent(R.id.btn_ignore, ignoreIntent);//
        //创建通知:
        String id = "channel_001";
        String name = "name";
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        notification = new Notification.Builder(mContext, id)
//            .setChannelId(id)
//            .setContentTitle("活动")
//            .setContentText("您有一项新活动")
            .setCustomBigContentView(views)
            .setCustomContentView(views)
            //.setContent(views)//设置布局
            .setSmallIcon(R.drawable.bg_item_normal).build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(mChannel);
        }
        notificationManager.notify(AppConstants.NOTIFICATION_ID, notification);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值