Android 3種Notification使用方法

 

Notification可以讓我們在獲得消息的時候在狀態欄,以及鎖屏界面顯示對應的信息,接下來介紹3種Notification,分別是普通Notification,摺疊式Notification,懸掛式Notification。

Notification 的創建主要涉及到 Notification.Builder 、 Notification 、 NotificationManager 。

  1. Notification.Builer : 使用建造者模式構建 Notification 對象。由於 Notification.Builder 僅支持 Android 4.1及之後的版本,爲了解決兼容性問題, Google 在 Android Support v4 中加入了 NotificationCompat.Builder 類。對於某些在 Android 4.1 之後才特性,即使 NotificationCompat.Builder 支持該方法,在之前的版本中也不能運行。點我 查看更多關於 Notification 兼容性問題處理。
  2. Notification : 通知對應類,保存通知相關的數據。NotificationManager 向系統發送通知時會用到。
  3. NotificationManager : NotificationManager 是通知管理類,它是一個系統服務。調用 NotificationManager 的 notify() 方法可以向系統發送通知。

首先我們先創建一個NotificationManager :

        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

1.​​​​​普通Notification 

Notification.Builder builder=new Notification.Builder(MainActivity.this);
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);

                builder.setContentIntent(pendingIntent);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
                builder.setAutoCancel(true);
                builder.setContentTitle("這是普通Notification");
                builder.setContentText("這是通知內容");
                builder.setWhen(System.currentTimeMillis());

                Notification notification=builder.build();
                manager.notify(1,notification);

效果:

2.摺疊Notification

Notification.Builder builder=new Notification.Builder(MainActivity.this);
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);

                builder.setContentIntent(pendingIntent);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
                builder.setAutoCancel(true);
                builder.setContentTitle("這是摺疊Notification");
                builder.setContentText("這是通知內容");
                builder.setWhen(System.currentTimeMillis());
                /**這是與普通Notification不同的地方
                 * 用了RemoteViews*/
                RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.view_hold);
                Notification notification=builder.build();

                /**展開的視圖*/
                notification.bigContentView=remoteViews;
                manager.notify(2,notification);

效果:

3.懸掛Notification

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);
                /**懸掛的設置*/
                Intent hangIntent=new Intent();
                hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent hangPendingIntent=PendingIntent.getActivity(MainActivity.this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
                Notification.Builder builder = new Notification.Builder(MainActivity.this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        //點擊通知後自動清除
                        .setAutoCancel(true)
                        .setContentTitle("這是懸掛Notification")
                        .setContentText("這是通知內容")
                        .setWhen(System.currentTimeMillis())
                        //如果加上下面這句,通知就不會自動消失,需要用戶上滑才能消失,來電顯示即是如此
                        //.setFullScreenIntent(hangPendingIntent,true)
                        .setDefaults(~0)//這兩句是使懸掛通知自動消失
                        .setPriority(Notification.PRIORITY_HIGH)//這兩句是使懸掛通知自動消失
                        .setContentIntent(pendingIntent);

                //發送通知
                manager.notify(3, builder.build());

效果:

 

參考資料:Android Notification 詳解

                  懸掛通知不自動消失問題

                  Android Notification 的四種使用方式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章