1. 程式人生 > >Android學習筆記8-使用通知(Notification)

Android學習筆記8-使用通知(Notification)

Android學習筆記8-使用通知(Notification)

1.Notification簡介

通知是Android系統的一種特色的功能,當某個app希望給使用者提示資訊,但是該app又不在執行在前臺時,就可以利用通知。
傳送一條通知後,手機上方的狀態列就會顯示一個小圖示,下拉狀態列,會顯示通知的具體資訊。

實現程式碼

public class MainActivity extends AppCompatActivity implements View.OnClickListener{



    @Override
    protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendNotice = (Button) findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View v)
{ switch(v.getId()) { case R.id.send_notice: String CHANNEL_ID = "my_channel_01"; CharSequence name = getString(R.string.channel_01); String description = "你好世界"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationManager manager =
(NotificationManager) getSystemService (NOTIFICATION_SERVICE); //Android 8.0開始需要為通知設定channel NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance); channel.setDescription(description); channel.enableLights(true); channel.setLightColor(Color.RED); channel.enableVibration(true); channel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400}); manager.createNotificationChannel(channel); Notification notification = new Notification.Builder(this) //設定通知的標題 .setContentTitle("This is content title") // 設定通知的詳細資訊 .setContentText("打到皇家馬德里") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher )) .setChannelId(CHANNEL_ID) .build(); manager.notify(1,notification ); break; default:break; } } }

**從Android O開始 每個通知必須為其設定對應的channel **

channel的建構函式有三個引數 channel的id,name,和優先順序。 建立完channel後通過 NotificationManager的createNotificationChannel(channel)方法新增chanel,並在在notification裡設定setChannelId。

通常 通知會在廣播接收器(broadcastReciver)或者服務(Server)中建立,不在活動中建立,因為app給我們傳送通知的時候並不是執行在前臺。

通過點選通知跳轉活動

如果我們想要使用者點選app發出的通知有反應,比如進入進入一個頁面,這裡就需要用到pendingIntent.
PendingIntent和Intent很類似,都可以用於啟動活動,啟動服務,以及傳送廣播。但是pendingIntent更傾向於某個合適的時機執行某個動作,相當於延遲型別的Intent.
                //設定pendingIntent讓使用者點選通知時跳轉活動。
                Intent intent = new Intent(this,NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this,0 ,intent ,0 );

獲取PendingIntent例項可以通過PendingIntent的getActivity(),getBroadCast(),getService()方法
第一個引數是Context,第二個引數一般為0,第三個是Intent物件,第四個是確定pendingIntent的行為,有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCLE_CURRENT,FLAG_UPDATE_CURRENT四種值,一般傳入0

最後通過NotificationCompat的setContentIntent(pi)加上點選功能。

                Notification notification = new NotificationCompat.Builder(this)
                      .setContentTitle("This is content title")
                      .setContentText("打到皇家馬德里")
                      .setWhen(System.currentTimeMillis())
                      .setSmallIcon(R.mipmap.ic_launcher)
                      .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher ))
                      .setChannelId(CHANNEL_ID)
                      .setContentIntent(pi)
                      .build();

設定當用戶點選完通知操作後,通知圖示就消失

第一種方法

當點選了通知後,通知會自動取消掉。

第二種方法,在跳轉的活動裡設定
public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }
}

這裡manager.cancel()傳入的引數是給每條引數指定的id.

2.Notification進階技巧


需要宣告許可權

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

也可以直接預設設定

3.Notification的高階技巧

設定通知欄詳情的長文字內容

顯示大圖片

設定通知的重要程度

  • PRIORITY_DEFAULT 預設的重要程度
  • PRIORITY_MIN 表示最低的重要程度,系統可能只會在特定的場景才會顯示這條通知
  • PRIORITY_LOW 表示較低的重要程度
  • PRIORITY_HIGH 表示較高的重要程度,系統可能會將這類通知放大,或改變其顯示順序,比較靠前的位置
  • PRIORITY_MAX 最高的重要程度,必須讓使用者立刻看到,甚至做出相應的操作。