Android8.0 Notification Demo

在Android8.0版本中,Notification较之前版本有所变化,主要是加入了NotificationChannel。

文章中主要举例:NormalStyle,BigTextStyle,BigPictureStyle,InputStyle,CustomViewStyle。

文中是部分代码,如果大家参考代码的话,请参见github代码:

NotificationDemo

下面请看代码:正常的通知样式。

@RequiresApi(api = Build.VERSION_CODES.O)
    public void sendNotification(View view) {
        String id = "channel_0";
        String des = "111";
        NotificationChannel channel = new NotificationChannel(id, des, NotificationManager.IMPORTANCE_MIN);
        notificationManager.createNotificationChannel(channel);
        Notification notification = new Notification.Builder(MainActivity.this, id)
                .setContentTitle("Base Notification View")
                .setContentText("您有一条新通知")
                .setSmallIcon(R.drawable.jd_icon)
                .setStyle(new Notification.MediaStyle())
                .setAutoCancel(false)
                .addExtras(new Bundle())
                .build();
        notificationManager.notify(1, notification);
    }

创建大文本样式Notification.BigTextStyle:

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void sendNotification_O() {
        intent = new Intent(MainActivity.this, LoginActivity.class);
        PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
        String id = "channel_1";
        String description = "123";
        NotificationChannel mChannel = new NotificationChannel(id, "123", NotificationManager.IMPORTANCE_HIGH);
        mChannel.setDescription(description);
        notificationManager.createNotificationChannel(mChannel);
        Notification notification = new Notification.Builder(MainActivity.this, id).setContentTitle("Title")
                .setSmallIcon(R.drawable.jd_icon)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jingdong_icon))
                .setContentTitle("您有一条新通知")
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setContentText("这是一条逗你玩的消息")
                .setStyle(new Notification.BigTextStyle()
                        .bigText(getString(R.string.dialog_message)))
                .setAutoCancel(false)
                .addExtras(new Bundle())
                .setContentIntent(pintent)
                .build();
        notificationManager.notify(1, notification);
    }

创建大图样式Notification.BigPictureStyle:

    public void sendNotificationBigPic(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent = new Intent(MainActivity.this, LoginActivity.class);
            PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
            Icon icon = Icon.createWithResource(this, R.drawable.jd_icon);
            Notification.Action action1 = new Notification.Action.Builder(icon, "Action1", pintent).build();
            String cancelId = "channel_2";
            NotificationChannel cannel = new NotificationChannel(cancelId, "456", NotificationManager.IMPORTANCE_MIN);
            cannel.setDescription("456_des");
            notificationManager.createNotificationChannel(cannel);
            Notification notification = new Notification.Builder(this, cancelId)
                    .setSmallIcon(R.drawable.jd_icon)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jingdong_icon))
                    .setContentTitle("您有一条新通知1")
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setContentText("这是一条逗你玩的消息1")
                    .setStyle(new Notification.BigPictureStyle()
                            .bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.fm_ting_card)))
                    .setAutoCancel(false)
                    .setContentIntent(pintent)
                    .addAction(action1)
                    .build();
            notificationManager.notify(2, notification);
        }
    }

创建快速回复输入框:

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void sendNotificationInput(View view) {
        // Key for the string that's delivered in the action's intent.
        String KEY_TEXT_REPLY = "key_text_reply";

        String replyLabel = getResources().getString(R.string.reply_label);
        RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
                .setLabel(replyLabel)
                .build();

        intent = new Intent(MainActivity.this, LoginActivity.class);
        PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);

        // Build a PendingIntent for the reply action to trigger.
        PendingIntent replyPendingIntent =
                PendingIntent.getActivity(getApplicationContext(),
                        100,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
        // Create the reply action and add the remote input.
        Notification.Action action =
                new Notification.Action.Builder(R.drawable.jd_icon,
                        getString(R.string.label), replyPendingIntent)
                        .addRemoteInput(remoteInput)
                        .build();
        // Build the notification and add the action.
        Notification newMessageNotification = new Notification.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.jd_icon)
                .setContentTitle(getString(R.string.title_activity_login))
                .setContentText(getString(R.string.error_invalid_email))
                .addAction(action)
                .build();
        // Issue the notification.
        notificationManager.notify(3, newMessageNotification);
    }

创建一个自定义样式:

    public void sendNotificationCustomView(View view) {
        // Get the layouts to use in the custom notification
        RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
        RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

// Apply the layouts to the notification
        Notification customNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.jd_icon)
                //如果不显示icon和title,那么就把下面这句去掉
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded)
                .build();
        notificationManager.notify(7, customNotification);
    }

 

以上是部分代码,如果大家参考代码的话,请参见github代码:

NotificationDemo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值