日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android - 通知Notification

發布時間:2023/12/29 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android - 通知Notification 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

指 Android 在應用的界面之外顯示的消息,旨在向用戶提供提醒、來自他人的通信信息或應用中的其他實時信息。用戶可以點擊通知來打開應用,也可以直接在通知中執行某項操作,比如點擊按鈕可以切歌,甚至在通知欄上直接回復消息。

顯示位置

  • 狀態欄和通知欄
    在狀態欄上顯示通知圖標,在通知欄顯示詳細內容,用戶點擊通知欄里面的通知一般會跳轉到應用相應頁面。
  • 屏幕上方
    當未鎖屏時通知可以顯示在屏幕上面,可以伴隨著提示音或者震動,提示一會后如果用戶沒有處理會自動消失
  • 鎖屏顯示
    當屏幕鎖定時,通知可以顯示在鎖屏界面上,并且伴隨亮屏,用戶可根據通知等級控制可顯示的通知
  • 應用圖標
    在一些設備上,通知可以顯示在應用圖標上,一般在右上方顯示一個數字代表該應用有多少通知用戶未查看,用戶可以長按應用圖標查看通知列表。
  • 概念

  • 渠道:從 Android 8.0(API 級別 26)開始,必須為所有通知分配渠道,否則通知將不會顯示。用戶可以根據渠道接收想要接收的通知
  • 重要程度: Android 7.1(API 級別 25)及更低版本的設備上,每條通知的重要程度均由通知的 priority 決定;在搭載 Android 8.0(API 級別 26)及更高版本的設備上,通知的重要程度由通知發布到的渠道的 importance 決定。通知的重要程度不同,提示用戶的方式就不同,重要度越高提示越明顯。
  • 前臺服務的通知:在服務內發送的前臺通知,用戶無法移除,除非關閉服務或者在服務內部關閉該通知
  • 勿擾模式:從 Android 5.0(API 級別 21)開始,用戶可以啟用勿擾模式,來控制被打擾的情況,指定可以打擾的類型。
  • 簡單使用

  • 創建通道
  • @TargetApi(Build.VERSION_CODES.O) public static void createNotificationChannel(Context ctx, String channelId, String channelName, String channelDes) {NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);channel.setDescription(channelDes);channel.setSound(null, null);channel.enableLights(true);channel.setLightColor(Color.RED);channel.setShowBadge(true);NotificationManager notifyMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);notifyMgr.createNotificationChannel(channel); }
  • 創建通知
  • //用戶點擊通知的意圖 Intent intent = new Intent(mContext, MainActivity.class); //通知中的交互都用延遲意圖 PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);//添加按鈕點擊意圖,在通知里面的按鈕點擊必須都用過broadcast,在broadcast處理點擊事件 Intent btnIntent = new Intent(mContext, MusicReceiver.class); btnIntent.setAction("com.dean.smartapp.broadcast.music"); btnIntent.putExtra("data", "notification data"); PendingIntent btnPendingIntent = PendingIntent.getBroadcast(mContext, 0, btnIntent, 0);//使用之前的通道 NotifyUtils.NOTIFY_CHANNEL_MUSIC 創建一個通知,如果較低版本不需要通道,NotificationCompat會自動適配 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NotifyUtils.NOTIFY_CHANNEL_MUSIC); builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知標題").setContentText("這是一條通知的內容")//設置通知的展開內容.setStyle(new NotificationCompat.BigTextStyle().bigText("這是通知的擴展內容,可以是文本擴展或者其他擴展內容,在通知欄可以直接查看"))//設置通知的重要等級以影響通知用戶的方式.setPriority(NotificationCompat.PRIORITY_DEFAULT)//設置用戶點擊通知后的意圖.setContentIntent(pendingIntent)//用戶點擊后自動移除.setAutoCancel(true)//添加按鈕,默認最多只能添加三個按鈕,而不影響通知本身的正常點擊跳轉Activity.addAction(R.mipmap.ic_launcher, "通知按鈕1", btnPendingIntent).addAction(R.mipmap.ic_launcher, "通知按鈕2", btnPendingIntent).addAction(R.mipmap.ic_launcher, "通知按鈕3", btnPendingIntent);
  • 顯示通知
  • NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(mContext); notificationManagerCompat.notify(0, builder.build());

    在通知欄直接輸入文本

    在Android 7.0(API 級別 24)允許用戶直接在通知中輸入文本,然后會直接提交給應用,而不必打開 Activity。比如聊天軟件可以在通知欄直接回復(對比了iOS后,哎,效果天壤之別,希望谷歌能夠優化吧)

    首先創建RemoteInput用來顯示輸入框,接收用戶輸入的文字 RemoteInput remoteInput = new RemoteInput.Builder("Key值用來取出用戶輸入的數據").setLabel("輸入框默認文字").build();使用Broadcast來接收文字 Intent inputIntent = new Intent(mContext, MusicReceiver.class); inputIntent.setAction("com.dean.smartapp.broadcast.music"); inputIntent.putExtra("data", "notification data"); PendingIntent inputPendingIntent = PendingIntent.getBroadcast(mContext, 0, inputIntent,PendingIntent.FLAG_UPDATE_CURRENT);創建action NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "按鈕名稱", inputPendingIntent).addRemoteInput(remoteInput).build();Intent intent = new Intent(mContext, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NotifyUtils.NOTIFY_CHANNEL_MUSIC); builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知標題").setContentText("這是一條通知的內容").setContentIntent(pendingIntent).setAutoCancel(true)//將action添加到通知上.addAction(action); 顯示通知 NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(mContext); notificationManagerCompat.notify(0, builder.build());最后在Receiver中接收文字并且處理,如果需要在通知上顯示用戶新輸入的文字,即發送一個新通知 注意發送通知的flag要和之前一樣,用來覆蓋之前的通知 public class MusicReceiver extends BroadcastReceiver {private static final String LOG_TAG = MusicReceiver.class.getSimpleName();@Overridepublic void onReceive(Context context, Intent intent) {Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);if (remoteInput != null) {Log.d(LOG_TAG, "用戶通知欄輸入 data = " + remoteInput.getCharSequence("a"));}} }

    自定義視圖

    可以使用setCustomContentView和setCustomBigContentView來為通知設置自定義視圖,在通知中可以通過長按來切換這兩種樣式

    Notification自定義view使用RemoteViews RemoteViews smallRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_small_layout); RemoteViews bigRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_big_layout);Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID).setSmallIcon(R.drawable.notification_icon).setStyle(new NotificationCompat.DecoratedCustomViewStyle()).setCustomContentView(smallRemoteViews ).setCustomBigContentView(bigRemoteViews).build();

    運行效果

    添加發送人

    在Android P版本以上需要Person來讓通知達到最佳呈現,即誰發送了這個通知,它在不支持的設備上無效。

    //先創建一個Person Person person = new Person.Builder().setName("胡漢三").setIcon(IconCompat.createWithResource(mContext, R.mipmap.ic_launcher)).build();//在創建MessaginStyle,如果多人,可以使用setGroupConversation標記為一個組 NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(person);style.addMessage("這是胡漢三發送的通知", System.currentTimeMillis(), person);style.setConversationTitle("胡漢三發送通知啦");將他設置給notification 正常顯示通知即可 NotificationCompat.Builder.setStyle(style)

    其他功能

  • 關于渠道,應用應該為一類的通知開啟單獨的渠道,以便用戶可以根據渠道管理來控制應用中的通知
  • 同一類通知可以添加分組
  • NotificationCompat.Builder.setGroup(GROUP_KEY_WORK_EMAIL) 為分組添加描述 NotificationCompat.Builder.setStyle(new NotificationCompat.InboxStyle().addLine("Alex Faarborg Check this out").addLine("Jeff Chang Launch Party").setBigContentTitle("2 new messages").setSummaryText("dean")) NotificationCompat.Builder.setGroupSummary(true)
  • Builder的setStyle()可以為通知設置許多展開后的樣式,比如展開大文本、展開大圖,甚至是音樂播放器的一些播控,但是谷歌提供的默認樣式不一定適用,這時可用自定義樣式來處理
  • notificationManagerCompat.notify顯示通知時可以使用相同的id來覆蓋之前的通知,為了避免提示用戶的方式多次發生,可以使用setOnlyAlertOnce()來設置只提醒一次。
  • 移除通知的方式除了用戶處理,代碼中可通過cancelAll() 來取消所有通知,或者使用setTimeoutAfter() 為通知設置超時,當時間到了自動消失
  • 可以通過setVisibility()來控制通知在鎖屏時的顯示內容。取值有三個:
  • VISIBILITY_PUBLIC 顯示通知的完整內容。 VISIBILITY_SECRET 不在鎖定屏幕上顯示該通知的任何部分。 VISIBILITY_PRIVATE 顯示基本信息,例如通知圖標和內容標題,但隱藏通知的完整內容
  • 需要顯示進度條時可以使用setProgress
  • builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false); 然后通過ID發送相同的通知來更新進度 如果完成后需要取消進度條,需要設置 .setProgress(0,0,false);
  • 關于PendingIntent最后一個參數FLAG
  • FLAG_ONE_SHOT:獲取的PendingIntent只能使用一次 FLAG_NO_CREATE:利用FLAG_NO_CREAT獲取的PendingIntent,若描述的Intent不存在則返回NULL值 FLAG_CANCEL_CURRENT:如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的 FLAG_UPDATE_CURRENT:能夠新new一個 Intent

    總結

    以上是生活随笔為你收集整理的Android - 通知Notification的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。