2017年5月30日 星期二

Android Notification 的幾種樣式

推播內容加上一張小圖片


private void sendNotificationWithSmallIcon(String title, String messageBody)
{
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.user);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(largeIcon)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(messageBody);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}


推播內容加上大圖加上下方的按鈕



//載入bitmap
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.user);
//設定大圖Style
NotificationCompat.BigPictureStyle bitStyle = new NotificationCompat.BigPictureStyle();
bitStyle.bigPicture(largeIcon);
//設定按鈕
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.addAction(R.mipmap.ic_launcher, "Reply", replyIntent)

private void sendNotificationWithButtonAndLargeIcon(String title, String messageBody)
{
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("ACTION_TYPE", "Reply");
Intent intent2 = new Intent(this, MainActivity.class);
intent2.putExtra("ACTION_TYPE", "Delete");
PendingIntent replyIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deleteIntent = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.user);
NotificationCompat.BigPictureStyle bitStyle = new NotificationCompat.BigPictureStyle();
bitStyle.bigPicture(largeIcon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(bitStyle)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(messageBody)
.addAction(R.mipmap.ic_launcher, "Reply", replyIntent)
.addAction(R.mipmap.ic_launcher, "Delete", deleteIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}


推播內容使用客製的View



//new RemoteViews 載入客製layout
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_view);
//設定使用CustomView
notificationBuilder.setCustomBigContentView(remoteViews);


private void sendNotificationWithCustomView(String title, String messageBody)
{
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_view);
remoteViews.setTextViewText(R.id.title, title);
remoteViews.setTextViewText(R.id.desc, messageBody);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent detailIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.putExtra("ACTION_TYPE", "Detail");
PendingIntent replyIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.putExtra("ACTION_TYPE", "Reply");
PendingIntent deleteIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.putExtra("ACTION_TYPE", "Delete");
remoteViews.setOnClickPendingIntent(R.id.reply, detailIntent);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(messageBody);
if (android.os.Build.VERSION.SDK_INT >= 16) {
notificationBuilder.setCustomBigContentView(remoteViews);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

客製view
notification_view.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="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="14dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="This is a mail title"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text=""
android:textSize="12sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal">
<Button
android:id="@+id/reply"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#00000000"
android:drawableLeft="@mipmap/reply"
android:drawablePadding="2dp"
android:gravity="left|center_vertical"
android:padding="0dp"
android:text="Reply" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#00000000"
android:drawableLeft="@mipmap/delete"
android:drawablePadding="2dp"
android:gravity="left|center_vertical"
android:padding="0dp"
android:text="Delete" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="130dp"
android:layout_height="130dp"
android:src="@mipmap/user" />
</LinearLayout>
</LinearLayout>


完整程式碼:https://github.com/evanchen76/PushNotificationSample

參考:https://developer.android.com/guide/topics/ui/notifiers/notifications.html

沒有留言:

張貼留言