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

直接上代码:

  /**
     * 普通通知
     */
    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.csdn.net/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.csdn.net/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.csdn.net/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);
            }
        }
    }

 

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