欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用Notification實現(xiàn)通知功能

 更新時間:2021年11月25日 10:20:54   作者:blue33sun  
這篇文章主要為大家詳細介紹了Android使用Notification實現(xiàn)通知功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這篇文章并未詳細介紹通知相關(guān)的api,而是作者自己對通知的一些大致總結(jié),以便日后查看,請讀者自行參考閱讀。

andorid關(guān)于通知在多個sdk版本中均有修改,因此部分api涉及到版本兼容的問題。編程中我們使用NotificationCompat來實現(xiàn)通知的相關(guān)功能。

1.通知中添加按鈕的方式

  • Notification可以通過直接調(diào)Notification.Builder.addAction(int icon, CharSequence title, PendingIntent intent)或者Notification.Builder.addAction(Action action)來添加按鈕
  • 可以通過設(shè)置RemoteView自定義布局的方式來添加按鈕;

2.通知的各種style

如果普通的通知樣式無法滿足項目需求,我們可以使用android提供的各種style。
目前style的種類包括BigTextStyle(超長文本)、InboxStyle(多行/列表)、BigPictureStyle(大圖片)、MessagingStyle(多條消息)、MediaStyle(started Android Oreo)。

3.自定義的通知View

如果上面普通通知欄和各種style不能滿足需求,也可以自己定義通知欄視圖remoteVIew,并將其設(shè)置給通知的ContentView即可。在android隨后更新的sdk版本中增加了BigContentView(started android Jelly_bean)、heasUpContentView(started android Lollipop),分別用于顯示通知欄的大視圖,懸掛視圖。

4.鎖屏?xí)r展示通知

自android Lollipop版本開始支持鎖定屏幕時顯示通知。用戶可以通過“設(shè)置”選擇是否將通知顯示在鎖定屏幕上,并且您可以指定您應(yīng)用中的通知在鎖定屏幕上是否可見。通過 setVisibility() 并指定以下值之一:

  • VISIBILITY_PUBLIC 顯示通知的完整內(nèi)容。
  • VISIBILITY_SECRET 不會在鎖定屏幕上顯示此通知的任何部分。
  • VISIBILITY_PRIVATE 顯示通知圖標和內(nèi)容標題等基本信息,但是隱藏通知的完整內(nèi)容。設(shè)置 VISIBILITY_PRIVATE 后,您還可以提供其中隱藏了某些詳細信息的替換版本通知內(nèi)容。例如,短信 應(yīng)用可能會顯示一條通知,指出“您有 3 條新短信”,但是隱藏了短信內(nèi)容和發(fā)件人。要提供此替換版本的通知,請先使用 NotificationCompat.Builder 創(chuàng)建替換通知。創(chuàng)建專用通知對象時,請通過 setPublicVersion() 方法為其附加替換通知。

5.快捷回復(fù)

自android Nougat版本開始增加了通知欄的快捷回復(fù)功能,具體實現(xiàn)步驟:給通知欄添加一個action(一般是快捷服務(wù)按鈕),該action初始化時傳入PendingIntent和RemoteInput即可。

//通知快速回復(fù)
public void quickReplyClick(View view){
        NotificationCompat.Builder builder = getBuilder();

        Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteInput remoteInput = new RemoteInput
                .Builder("RemoteInputKey")
                .setLabel("RemoteInputLabel")
                .build();

        NotificationCompat.Action action = new NotificationCompat.Action
                .Builder(R.drawable.air, "回復(fù)", pendingIntent)
                .addRemoteInput(remoteInput)
                .build();

        builder.addAction(action);

        // 發(fā)送該通知
        notifyDefaultPriority(++mNotificationId,builder);
    }

6.通知分組功能

自android Nougat版本開始增加了通知的分組功能。在android Nougat版本及以上,如果同一應(yīng)用發(fā)出 4 條或更多條通知且未指定分組,則系統(tǒng)會自動將這些通知分為一組。

通知分組的實現(xiàn)方式(只列舉關(guān)鍵方法):

private final String GROUP_NOTIFICATION_ONE = "GROUP1";
private final int  GROUP_NOTIFICATION_ID = 0;

    //通知分類
    public void classifyClick(View view){
        NotificationCompat.Builder builder1 = getBuilder();//創(chuàng)建一個普通的通知buidler對象,方法很簡單
        builder1.setGroup(GROUP_NOTIFICATION_ONE);//設(shè)置group
        notifyDefaultPriority(++mNotificationId,builder1);//彈出第一條通知
        notifyDefaultPriority(++mNotificationId,builder1);//彈出第二條通知

        NotificationCompat.Builder builder2 = getBuilder();//創(chuàng)建一個普通的通知buidler對象,方法很簡單
        builder2.setContentTitle("test classify");
        builder2.setGroup(GROUP_NOTIFICATION_ONE);//設(shè)置相同的group
        builder2.setGroupSummary(true);//這一句必須要,這條通知是作為summary notification(我的理解是將已經(jīng)發(fā)送的相同group的通知進行歸類)
        notifyDefaultPriority(GROUP_NOTIFICATION_ID,builder2);//這條通知的notification id是個常量,彈出通知
    }

7.通知通道NotificationChannel

自android Oreo版本開始增加了通知通道的概念,在targetSdkVersion>=26時彈出通知需要做兼容處理:為Notification設(shè)置channel,否則通知將會彈出失敗。

private void setNotifyChannel(NotificationCompat.Builder builder,String channelId,String channelName,int importance){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = nm.getNotificationChannel(channelId);
            if(notificationChannel==null){
                notificationChannel = new NotificationChannel(channelId,channelName,importance);
                nm.createNotificationChannel(notificationChannel);
            }
            builder.setChannelId(channelId);
        }
    }

8.通知的重要程度

Android 利用通知的重要程度來決定通知應(yīng)在多大程度上干擾用戶(視覺上和聽覺上)。通知的重要程度越高,干擾程度就越高。

  • 在搭載 Android Oreo(API 級別 26)及更高版本的設(shè)備上,通知的重要程度由通知發(fā)布到的渠道NotificationChannle的 importance 決定。用戶可以在系統(tǒng)設(shè)置中更改通知渠道的重要程度。
  • 在搭載 Android 7.1(API 級別 25)及更低版本的設(shè)備上,每條通知的重要程度均由通知的 priority 決定。

以上便是通知的相關(guān)知識~
其他可參考官網(wǎng)

這里增加說明下可能會觸發(fā)懸浮式通知的條件示例:

  • 用戶的 Activity 處于全屏模式(應(yīng)用使用 fullScreenIntent)。
  • 通知的優(yōu)先級很高,且在搭載 Android 7.1(API 級別 25)及更低版本的設(shè)備上使用鈴聲或振動。
  • 在搭載 Android 8.0(API 級別 26)及更高版本的設(shè)備上,通知渠道的重要程度比較高。

對應(yīng)的有三種實現(xiàn)方式:

為Notification設(shè)置全屏?xí)r的PendingIntent:setFullScreenIntent(PendingIntent intent, boolean highPriority)即可(第二個參數(shù)表示是否是高優(yōu)先級,需傳值true。在android Oreo的平臺上則需要NotificationChannel的優(yōu)先級設(shè)置為IMPORTANCE_HIGH或者IMPORTANCE_MAX才有效)

為Notification設(shè)置優(yōu)先級setPriority(NotificationCompat.PRIORITY_HIGH)或者builder.setPriority(NotificationCompat.PRIORITY_MAX)(在android Oreo的平臺上只需要NotificationChannel的優(yōu)先級設(shè)置為IMPORTANCE_HIGH或者IMPORTANCE_MAX),同時android 7.1及以下的平臺還需要設(shè)置setSound(Uri sound)才行(android 8.0平臺無需設(shè)置震動或鈴聲)

在android Lollipop及以上平臺,可以自定義懸掛視圖remoteView,將其設(shè)置為Notification的heasUpContentView即可。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論