Notificationの高度な使用(8.0対応)


目次


一、引用
二、効果
三、8.0通知欄適合
四、普通Notification
五、折りたたみNotification
六、吊り下げNotification
七、Notificationの表示レベル

一、引用


以前のブログでは、純粋なテキストからマルチビュー、音声、振動まで、Notificationの基本的な使用といくつかの設定が記録されていました.添付リンク:https://blog.csdn.net/qi85481455/article/details/82895507 .携帯電話システムのアップグレードに伴い、GoogleはNotificationをいくつかアップグレードし、このブログにはアップグレード後の通知欄の使用が記録されています.ここでは主に3つの通知欄の使用(一般通知欄,折りたたみ通知欄,吊り下げ通知欄)および8.0通知欄の適合について述べる.

二、効果


三、8.0通知欄適合


  NotificationChannelはandroid 8.0新しい機能は、AppのtargetSDKVersion>=26で、channel通知チャネルが設定されていないと、通知が表示されなくなります.Android Oは、android Oをターゲットとするプラットフォームであれば、ユーザーに通知を表示するために1つ以上の通知チャネルを実現する必要がある統一されたシステムを提供する通知チャネル(Notification Channels)を導入しています.例えば、チャットソフトウェアでは、チャットグループごとに通知チャネルを設定し、特定の音、ライトなどの構成を指定します.
 private void initNotificationBuild() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            // 
            channel.canBypassDnd();
            // 
            channel.enableLights(true);
            // 
            channel.setLockscreenVisibility(1);
            // 
            channel.setLightColor(Color.RED);
            // launcher 
            channel.canShowBadge();
            // 
            channel.enableVibration(true);
            // 
            channel.getAudioAttributes();
            // 
            channel.getGroup();
            //    
            channel.setBypassDnd(true);
            // 
            channel.setVibrationPattern(new long[]{100, 100, 200});
            // 
            channel.shouldShowLights();
            getNotificationManager().createNotificationChannel(channel);
        }
        builder = new NotificationCompat.Builder(this, "channel_id");
    }

ここでは8.0通知権限の詳しいブログを見つけて、興味があれば調べてみてください.

https://www.jianshu.com/p/b83fc1697232


四、普通Notification


まず、PendingIntentでジャンプを制御するBuildオブジェクトを作成します.
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Builderがあれば、Notificationにさまざまなプロパティを追加できます.
builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        builder.setContentTitle(" ");
        mNotificationManager.notify(0, builder.build());

五、折りたたみNotification


 折りたたみ式Notificationは、長いテキストといくつかのカスタムViewを表示するためのカスタムNotificationです.ここでは、展開状態のビューに表示されるプロセスと、ビューを作成するプロセスは1つのプロセスではないので、RemoteViewが必要です.
 // RemoteView Notification 
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        // 
        notification.bigContentView = remoteViews;

折りたたみ式の通知バーでない場合は、通常の通知バーと同じように表示されます.次は完全なコードです.
 /**
     *  
     */
    private void foldNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle(" ");
        // RemoteView Notification 
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        // 
        notification.bigContentView = remoteViews;
        mNotificationManager.notify(1, notification);
    }

六、吊り下げNotification


サスペンション式NotificationはAndroid 5.0が新たに追加された方式です.サスペンション式Notificationはドロップダウン通知欄を必要とせずに画面の上に直接表示され、焦点は変わらず、ユーザー操作インタフェースにあり、ユーザー操作を計画せず、数秒後に自動的に消える.主にsetFullScreenIntentを呼び出してNotificationをサスペンション通知に変更
 Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent,true);

サスペンションNotificationフルコード:
/**
     *  
     */
    private void suspendNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentTitle(" ");
        // 
        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent,true);
        mNotificationManager.notify(2,builder.build());
    }

七、Notificationの表示レベル


5.0 Notificationの管理にレベルを入れた表示
等級
説明
NotificationCompat.VISIBILITY_PRIVATE
通知は、画面がロックされていない場合にのみ表示されます.
NotificationCompat.VISIBILITY_PUBLIC
いずれの場合も通知が表示されます
NotificationCompat.VISIBILITY_SECRET
pin、passwordセキュリティロック、およびロックなしで通知を表示
使用方法:
  builder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
   builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
    builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

以上はNotificationのすべての内容で、あなたを助けることができることを望みます!