《Android进阶之光》3种通知 Notification实例

本文深入探讨了Android中三种主要的通知类型:普通通知、折叠通知和悬挂通知。通过代码实例展示了如何创建这些通知,并解释了每种通知的特点和应用场景。对于希望在Android应用中实现丰富通知功能的开发者来说,本文提供了实用的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码:

  /**
     * 普通通知
     */
    private void sendOrdinaryNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        Notification notification = null;

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.youkuaiyun.com/hfy8971613/article/details/85481124"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        notification = builder.setContentTitle("普通通知Title")
                .setContentText("普通通知Text")
                .setSmallIcon(R.mipmap.dog)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                //点击跳转
                .setContentIntent(pendingIntent)
                .build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //显示等级,任何情况 都会显示通知
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }

        if (notificationManager != null) {
            notificationManager.notify(1, notification);
        }

    }

    /**
     * 折叠通知
     * 就是自定义视图的通知; 这个视图显示的进程 和 创建视图的进程不是一个进程,所以我们需要使用 {@link RemoteViews}
     */
    private void sendFoldNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.youkuaiyun.com/hfy8971613/article/details/85481124"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.fold_notification);

        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification = builder.setContentTitle("折叠通知Title")
                    .setContentText("折叠通知Text")
                    .setSmallIcon(R.mipmap.dog)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    //点击跳转
                    .setContentIntent(pendingIntent)
                    //下拉前
//                        .setCustomContentView(collapsed)
                    //设置展开时的视图(下拉后)
                    .setCustomBigContentView(remoteView)
                    //设置浮动通知视图
//                        .setCustomHeadsUpContentView(remoteView)
                    .build();

            if (notificationManager != null) {
                notificationManager.notify(2, notification);
            }
        }
    }

    /**
     * 悬挂通知
     * 直接显示在 屏幕上方,且焦点不变,会自动消失。
     */
    private void sendHangNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        Notification notification = null;

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.youkuaiyun.com/hfy8971613/article/details/85481124"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Intent hangIntent = new Intent();
            hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            hangIntent.setClass(this, NotificationActivity.class);
            PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);

            notification = builder.setContentTitle("悬挂通知Title")
                    .setContentText("悬挂通知Text")
                    .setSmallIcon(R.mipmap.dog)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    //悬挂式通知  悬挂在手机屏上方
                    .setFullScreenIntent(hangPendingIntent, true)
                    .build();

            if (notificationManager != null) {
                notificationManager.notify(3, notification);
            }
        }
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值