《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);
            }
        }
    }

 

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