Android - 通知Notification
生活随笔
收集整理的這篇文章主要介紹了
Android - 通知Notification
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)介
指 Android 在應(yīng)用的界面之外顯示的消息,旨在向用戶提供提醒、來自他人的通信信息或應(yīng)用中的其他實(shí)時(shí)信息。用戶可以點(diǎn)擊通知來打開應(yīng)用,也可以直接在通知中執(zhí)行某項(xiàng)操作,比如點(diǎn)擊按鈕可以切歌,甚至在通知欄上直接回復(fù)消息。
顯示位置
在狀態(tài)欄上顯示通知圖標(biāo),在通知欄顯示詳細(xì)內(nèi)容,用戶點(diǎn)擊通知欄里面的通知一般會(huì)跳轉(zhuǎn)到應(yīng)用相應(yīng)頁(yè)面。
當(dāng)未鎖屏?xí)r通知可以顯示在屏幕上面,可以伴隨著提示音或者震動(dòng),提示一會(huì)后如果用戶沒有處理會(huì)自動(dòng)消失
當(dāng)屏幕鎖定時(shí),通知可以顯示在鎖屏界面上,并且伴隨亮屏,用戶可根據(jù)通知等級(jí)控制可顯示的通知
在一些設(shè)備上,通知可以顯示在應(yīng)用圖標(biāo)上,一般在右上方顯示一個(gè)數(shù)字代表該應(yīng)用有多少通知用戶未查看,用戶可以長(zhǎng)按應(yīng)用圖標(biāo)查看通知列表。
概念
簡(jiǎn)單使用
在通知欄直接輸入文本
在Android 7.0(API 級(jí)別 24)允許用戶直接在通知中輸入文本,然后會(huì)直接提交給應(yīng)用,而不必打開 Activity。比如聊天軟件可以在通知欄直接回復(fù)(對(duì)比了iOS后,哎,效果天壤之別,希望谷歌能夠優(yōu)化吧)
首先創(chuàng)建RemoteInput用來顯示輸入框,接收用戶輸入的文字 RemoteInput remoteInput = new RemoteInput.Builder("Key值用來取出用戶輸入的數(shù)據(jù)").setLabel("輸入框默認(rèn)文字").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);創(chuàng)建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("通知標(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要和之前一樣,用來覆蓋之前的通知 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來為通知設(shè)置自定義視圖,在通知中可以通過長(zhǎng)按來切換這兩種樣式
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來讓通知達(dá)到最佳呈現(xiàn),即誰發(fā)送了這個(gè)通知,它在不支持的設(shè)備上無效。
//先創(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)其他功能
總結(jié)
以上是生活随笔為你收集整理的Android - 通知Notification的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab在数组中插入,一次快速插入一
- 下一篇: PowerPMAC技术培训------7