Android Notification 你應該知道的事
阿新 • • 發佈:2019-02-04
1. 介紹
Notification :通知是可以顯示到你的應用程式的正常UI的使用者之外的訊息。當您告訴系統發出通知時,它首先會在通知區域顯示為一個圖示。要檢視通知的詳細資訊,使用者開啟通知抽屜。通知區域和通知抽屜都是使用者可以隨時檢視的系統控制區域。
2. 建立步驟
(1) 建立通知生成器:使用 NotificationCompat.Builder.build()
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
(2) 設定通知屬性(以下屬性必須設定)
- 一個小圖示,由setSmallIcon()設定
- 標題,由setContentTitle()設定
- 細節文字,由setContentText()設定
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
(3) 設定點選意圖:通過PendingIntent 進行設定
(4) 顯示通知
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());
3. 相容性
- Android 3.0 以下版本:
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity (context,0,new Intent(this,ResultActivity.class),0);
Notification notification = new Notification(icon,tickerText,when);
notification.setLatestEventInfo(this, title, content, pendingIntent);
notificationManager.notify(id,notification);
- Android 3.0 以上版本:
NotificationManager notificationManager =(NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(contentIntent)
.build();
notificationManager.notify(NOTIFICATIONS_ID, notification);
- support v4 相容版本(牆裂推薦使用該方式建立):
NotificationManager notificationManager =(NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(contentIntent)
.build();
notificationManager.notify(NOTIFICATIONS_ID, notification);
4. Notification 分類
- 普通式 Notification:
/**
* 相容 3.0 之前版本
*
* @param context
*/
public static void showNotificationCompat(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("url", "http://www.sina.com");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContentText("content")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(1, notification);
}
- 摺疊式 Notification:
摺疊式 Notification 是一種自定義檢視的 Notification,用來顯示自定義佈局。主要使用 RemoteViews 來建立自定義檢視。他有兩種狀態:第一次下滑為普通狀態,普通狀態下顯示結果與普通式 Notification 顯示的結果是一樣的;再次下滑為展開狀態,展開狀態下才會顯示自定義檢視。
//用RemoteViews來建立自定義Notification檢視
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
\res\layout\layout_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/tv_text"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/**
* 摺疊式 notification
* (1) 第一次下滑為普通狀態,這種狀態下和普通通知是一樣的
* (2) 再次下滑變為展開狀態,這種狀態下才顯示出自定義試圖
*/
public static void showFoldNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_notification);
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("url", "http://www.sina.com");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("摺疊式通知")
.setContentText("摺疊式通知內容")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setCustomBigContentView(remoteViews)
.build();
notificationManager.notify(1, notification);
}
- 懸掛式 Notification:
懸掛式 Notification 是 Android5.0 新增加的方式,懸掛式Notification不需要下拉通知欄就直接顯示出來懸掛在螢幕上方並且焦點不變仍在使用者操作的介面因此不會打斷使用者的操作,過幾秒就會自動消失。
他需要呼叫setFullScreenIntent來將Notification變為懸掛式Notification
public static void showHangingNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("url", "http://www.baidu.com");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent hangIntent = new Intent(context, MainActivity.class);
PendingIntent hangingPendingIntent = PendingIntent.getActivity(context, 11, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("懸掛式通知")
.setContentText("懸掛式通知內容")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setFullScreenIntent(hangingPendingIntent, true)
.build();
notificationManager.notify(1, notification);
}
5. 實際需求應用場景
當收到通知時,點選通知,根據通知攜帶內容攜帶的 URL 跳轉到詳情頁,在詳情頁點選返回時,返回到主介面 MainActivity,而不是退出 App。本案例不使用多次跳轉,而是使用 PendingIntent.getActivities() 方法進行解決。
- 首先將主介面 MainActivity 啟動模式設定為 SingleTask.
- Notification的setContentIntent()中的pendingIntent是通過PendingIntent.getActivities(Context,int,Intent[],int) 獲取的。
public static void showHandleNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent[] intents = new Intent[2];
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.putExtra("index", "22");
Intent webIntent = new Intent(context, WebViewActivity.class);
webIntent.putExtra("url", "http://www.baidu.com");
intents[0] = mainIntent;
intents[1] = webIntent;
PendingIntent pendingIntent = PendingIntent.getActivities(context, 11, intents, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notification")
.setContentIntent(pendingIntent)
.setContentText("content")
.build();
notificationManager.notify(11, notification);
}
注意:
(1) 點選 back 鍵,退出 App,收到 Notification 後,點選 Notification 跳轉到 WebViewActivity 載入網頁,點選 back 鍵,返回到 MainActivity,此時 MainActivity 生命週期如下(kill 掉程序,相同結果):
(2) 點選 home 鍵,收到 Notification,點選 Notification 跳轉到 WebViewActivity 載入網頁,點選 back 鍵,返回到 MainActivity,此時 MainActivity 生命週期如下:
程式碼片段:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.flaginfo.notificationdemo.MainActivity">
<Button
android:id="@+id/btn_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="普通式"/>
<Button
android:id="@+id/btn_folding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="摺疊式"/>
<Button
android:id="@+id/btn_hanging"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="懸掛式"/>
<Button
android:id="@+id/btn_handle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="Notification 跳轉"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivityMain";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if (intent != null) {
String index = intent.getStringExtra("index");
Log.d(TAG, "onCreate: index = " + index);
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
public void click(View view) {
switch (view.getId()) {
case R.id.btn_normal: // 普通式
NotificationHelper.showNotificationCompat(this);
break;
case R.id.btn_folding: // 摺疊式
NotificationHelper.showFoldNotification(this);
break;
case R.id.btn_hanging: // 懸掛式
NotificationHelper.showHangingNotification(this);
break;
case R.id.btn_handle:
NotificationHelper.showHandleNotification(this);
break;
default:
break;
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent: ");
if (intent != null) {
String index = intent.getStringExtra("index");
Log.d(TAG, "onNewIntent: index = " + index);
}
}
}
WeViewActivity.java
public class WebViewActivity extends AppCompatActivity {
private static final String TAG = "WebViewActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
webView.setWebChromeClient(new WebChromeClient() {
});
Intent intent = getIntent();
if (intent != null) {
String url = intent.getStringExtra("url");
webView.loadUrl(url);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(TAG, "onKeyDown: ");
Intent intent = new Intent();
intent.putExtra("index","1");
setResult(11,intent);
}
return super.onKeyDown(keyCode, event);
}
}