Notification自定义Layout(大视图)


自定义Layout带按钮

  private void popup() {
        //自定义布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
        //Button按钮
        Intent intent = new Intent("test");
        intent.putExtra("click", "您点击了Button");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btn_click, pendingIntent);

        //Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setOngoing(false);
        builder.setAutoCancel(false);
//        builder.setContent(remoteViews);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        //Notification
        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND;

        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.bigContentView = remoteViews;

        //NotificationManager
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //唤醒通知
        manager.notify(0, notification);
    }

自定义Layout不带按钮

 public void showNotification(SmsData data) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        // 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
        mBuilder.setSmallIcon(R.drawable.sms_notificantion);
        // mBuilder.setContentTitle("通知的标题");
        // mBuilder.setContentText("通知的内容");

        Notification notification = mBuilder.build();

        // 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
//        contentView.setImageViewResource(R.id.iv_sms_notification, R.drawable.sms_notificantion);

        contentView.setTextViewText(R.id.tv_notification_name, getContactsName(data));
        contentView.setTextViewText(R.id.tv_notification_address, data.address);
        notification.bigContentView = contentView;

        // 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
        // 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
        // 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;

        // 点击notification自动消失(注意:此处若使用FLAG_NO_CLEAR则上面的notification.bigContentView = contentView失效)
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        // 通知的时间
        notification.when = System.currentTimeMillis();

        // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
        Intent intent = new Intent(context, SmsActivity.class);

        // 当用户点击通知栏的Notification时候,切换回MainActivityPendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 /**
         * flags有四个取值:
         * int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
         *int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
         *int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
         *int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
         *我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。
         */
        notification.contentIntent = pi;

        // 发送到手机的通知栏
        notificationManager.notify(0, notification);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值