早期 API8 對 Notification 並未完全封裝內部變數, 因API版本不段演變, 舊有使用方式將被取消, 因此就需要改變該類的使用方式.
要帶入到Notification的資料(非重點)
訊息追加方式
from http://dev.classmethod.jp/smartphone/android/android-tips-23-android4-1-...
要帶入到Notification的資料(非重點)
►API-10 以前的用法int icon = R.drawable.ic_stat_gcm; long when = System.currentTimeMillis(); String contentTitle = context.getString(R.string.app_name); String message = "訊息內容"; // Intent notificationIntent = new Intent(context, MessageShow.class);//MessageShow顯示訊息的activity NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationIntent.putExtra(CommonUtilities.EXTRA_MESSAGE, message); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
►相容 API-10 以前的用法, 主要是改用 android-support-v4.jar 來上下版本相容Notification notification = new Notification(icon, message, when); notification.setLatestEventInfo(this, contentTitle, message, contentIntent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification);
►API-11 後的用法 (Notification已重新封裝)NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(icon); builder.setTicker("訊息通知:Ticker");//訊息提醒時顯示的提示文字 builder.setWhen(when); builder.setContentIntent(contentIntent); builder.setContentTitle(contentTitle);//"通知標題" builder.setContentText(message);//"通知摘要" builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); builder.setAutoCancel(true); notificationManager.notify(0, builder.build());//發出通知, 標準樣式
►不同呈現樣式Notification notification = new Notification.Builder(this) .setSmallIcon(icon) .setWhen(when) .setContentTitle(contentTitle) .setContentText(message) .setContentIntent(contentIntent) .getNotification(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(R.string.app_name, notification);
訊息追加方式
大文字標題樣式builder.addAction(icon, "訊息1", contentIntent); builder.addAction(icon, "訊息2", contentIntent);
多列清單樣式NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(builder); bigTextStyle.bigText("BigText"); bigTextStyle.setBigContentTitle("BigContentTitle"); bigTextStyle.setSummaryText("SummaryText"); notificationManager.notify(0, bigTextStyle.build());
圖片樣式NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder); inboxStyle.setBigContentTitle("BigContentTitle"); inboxStyle.setSummaryText("SummaryText"); for (int i = 0; i < 10; i++) { inboxStyle.addLine("Line" + (i + 1)); } notificationManager.notify(0, inboxStyle.build());
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(builder); bigPictureStyle.bigPicture(bigPicture); bigPictureStyle.setBigContentTitle("BigContentTitle"); bigPictureStyle.setSummaryText("SummaryText");
from http://dev.classmethod.jp/smartphone/android/android-tips-23-android4-1-...