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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android - 通知Notification

發(fā)布時(shí)間:2023/12/29 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android - 通知Notification 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

指 Android 在應(yīng)用的界面之外顯示的消息,旨在向用戶提供提醒、來(lái)自他人的通信信息或應(yīng)用中的其他實(shí)時(shí)信息。用戶可以點(diǎn)擊通知來(lái)打開(kāi)應(yīng)用,也可以直接在通知中執(zhí)行某項(xiàng)操作,比如點(diǎn)擊按鈕可以切歌,甚至在通知欄上直接回復(fù)消息。

顯示位置

  • 狀態(tài)欄和通知欄
    在狀態(tài)欄上顯示通知圖標(biāo),在通知欄顯示詳細(xì)內(nèi)容,用戶點(diǎn)擊通知欄里面的通知一般會(huì)跳轉(zhuǎn)到應(yīng)用相應(yīng)頁(yè)面。
  • 屏幕上方
    當(dāng)未鎖屏?xí)r通知可以顯示在屏幕上面,可以伴隨著提示音或者震動(dòng),提示一會(huì)后如果用戶沒(méi)有處理會(huì)自動(dòng)消失
  • 鎖屏顯示
    當(dāng)屏幕鎖定時(shí),通知可以顯示在鎖屏界面上,并且伴隨亮屏,用戶可根據(jù)通知等級(jí)控制可顯示的通知
  • 應(yīng)用圖標(biāo)
    在一些設(shè)備上,通知可以顯示在應(yīng)用圖標(biāo)上,一般在右上方顯示一個(gè)數(shù)字代表該應(yīng)用有多少通知用戶未查看,用戶可以長(zhǎng)按應(yīng)用圖標(biāo)查看通知列表。
  • 概念

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

  • 創(chuàng)建通道
  • @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); }
  • 創(chuàng)建通知
  • //用戶點(diǎn)擊通知的意圖 Intent intent = new Intent(mContext, MainActivity.class); //通知中的交互都用延遲意圖 PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);//添加按鈕點(diǎn)擊意圖,在通知里面的按鈕點(diǎn)擊必須都用過(guò)broadcast,在broadcast處理點(diǎn)擊事件 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 創(chuàng)建一個(gè)通知,如果較低版本不需要通道,NotificationCompat會(huì)自動(dòng)適配 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NotifyUtils.NOTIFY_CHANNEL_MUSIC); builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知標(biāo)題").setContentText("這是一條通知的內(nèi)容")//設(shè)置通知的展開(kāi)內(nèi)容.setStyle(new NotificationCompat.BigTextStyle().bigText("這是通知的擴(kuò)展內(nèi)容,可以是文本擴(kuò)展或者其他擴(kuò)展內(nèi)容,在通知欄可以直接查看"))//設(shè)置通知的重要等級(jí)以影響通知用戶的方式.setPriority(NotificationCompat.PRIORITY_DEFAULT)//設(shè)置用戶點(diǎn)擊通知后的意圖.setContentIntent(pendingIntent)//用戶點(diǎn)擊后自動(dòng)移除.setAutoCancel(true)//添加按鈕,默認(rèn)最多只能添加三個(gè)按鈕,而不影響通知本身的正常點(diǎn)擊跳轉(zhuǎn)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 級(jí)別 24)允許用戶直接在通知中輸入文本,然后會(huì)直接提交給應(yīng)用,而不必打開(kāi) Activity。比如聊天軟件可以在通知欄直接回復(fù)(對(duì)比了iOS后,哎,效果天壤之別,希望谷歌能夠優(yōu)化吧)

    首先創(chuàng)建RemoteInput用來(lái)顯示輸入框,接收用戶輸入的文字 RemoteInput remoteInput = new RemoteInput.Builder("Key值用來(lái)取出用戶輸入的數(shù)據(jù)").setLabel("輸入框默認(rèn)文字").build();使用Broadcast來(lái)接收文字 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);創(chuàng)建action NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "按鈕名稱(chēng)", 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("通知標(biāo)題").setContentText("這是一條通知的內(nèi)容").setContentIntent(pendingIntent).setAutoCancel(true)//將action添加到通知上.addAction(action); 顯示通知 NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(mContext); notificationManagerCompat.notify(0, builder.build());最后在Receiver中接收文字并且處理,如果需要在通知上顯示用戶新輸入的文字,即發(fā)送一個(gè)新通知 注意發(fā)送通知的flag要和之前一樣,用來(lái)覆蓋之前的通知 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來(lái)為通知設(shè)置自定義視圖,在通知中可以通過(guò)長(zhǎng)按來(lái)切換這兩種樣式

    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();

    運(yùn)行效果

    添加發(fā)送人

    在Android P版本以上需要Person來(lái)讓通知達(dá)到最佳呈現(xiàn),即誰(shuí)發(fā)送了這個(gè)通知,它在不支持的設(shè)備上無(wú)效。

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

    其他功能

  • 關(guān)于渠道,應(yīng)用應(yīng)該為一類(lèi)的通知開(kāi)啟單獨(dú)的渠道,以便用戶可以根據(jù)渠道管理來(lái)控制應(yīng)用中的通知
  • 同一類(lèi)通知可以添加分組
  • 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()可以為通知設(shè)置許多展開(kāi)后的樣式,比如展開(kāi)大文本、展開(kāi)大圖,甚至是音樂(lè)播放器的一些播控,但是谷歌提供的默認(rèn)樣式不一定適用,這時(shí)可用自定義樣式來(lái)處理
  • notificationManagerCompat.notify顯示通知時(shí)可以使用相同的id來(lái)覆蓋之前的通知,為了避免提示用戶的方式多次發(fā)生,可以使用setOnlyAlertOnce()來(lái)設(shè)置只提醒一次。
  • 移除通知的方式除了用戶處理,代碼中可通過(guò)cancelAll() 來(lái)取消所有通知,或者使用setTimeoutAfter() 為通知設(shè)置超時(shí),當(dāng)時(shí)間到了自動(dòng)消失
  • 可以通過(guò)setVisibility()來(lái)控制通知在鎖屏?xí)r的顯示內(nèi)容。取值有三個(gè):
  • VISIBILITY_PUBLIC 顯示通知的完整內(nèi)容。 VISIBILITY_SECRET 不在鎖定屏幕上顯示該通知的任何部分。 VISIBILITY_PRIVATE 顯示基本信息,例如通知圖標(biāo)和內(nèi)容標(biāo)題,但隱藏通知的完整內(nèi)容
  • 需要顯示進(jìn)度條時(shí)可以使用setProgress
  • builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false); 然后通過(guò)ID發(fā)送相同的通知來(lái)更新進(jìn)度 如果完成后需要取消進(jìn)度條,需要設(shè)置 .setProgress(0,0,false);
  • 關(guān)于PendingIntent最后一個(gè)參數(shù)FLAG
  • FLAG_ONE_SHOT:獲取的PendingIntent只能使用一次 FLAG_NO_CREATE:利用FLAG_NO_CREAT獲取的PendingIntent,若描述的Intent不存在則返回NULL值 FLAG_CANCEL_CURRENT:如果描述的PendingIntent已經(jīng)存在,則在產(chǎn)生新的Intent之前會(huì)先取消掉當(dāng)前的 FLAG_UPDATE_CURRENT:能夠新new一個(gè) Intent

    總結(jié)

    以上是生活随笔為你收集整理的Android - 通知Notification的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。