Android - 通知Notification
生活随笔
收集整理的這篇文章主要介紹了
Android - 通知Notification
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
指 Android 在應用的界面之外顯示的消息,旨在向用戶提供提醒、來自他人的通信信息或應用中的其他實時信息。用戶可以點擊通知來打開應用,也可以直接在通知中執行某項操作,比如點擊按鈕可以切歌,甚至在通知欄上直接回復消息。
顯示位置
在狀態欄上顯示通知圖標,在通知欄顯示詳細內容,用戶點擊通知欄里面的通知一般會跳轉到應用相應頁面。
當未鎖屏時通知可以顯示在屏幕上面,可以伴隨著提示音或者震動,提示一會后如果用戶沒有處理會自動消失
當屏幕鎖定時,通知可以顯示在鎖屏界面上,并且伴隨亮屏,用戶可根據通知等級控制可顯示的通知
在一些設備上,通知可以顯示在應用圖標上,一般在右上方顯示一個數字代表該應用有多少通知用戶未查看,用戶可以長按應用圖標查看通知列表。
概念
簡單使用
在通知欄直接輸入文本
在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)其他功能
總結
以上是生活随笔為你收集整理的Android - 通知Notification的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab在数组中插入,一次快速插入一
- 下一篇: Android--初级