使用 Picasso 庫設定自定義通知圖示
PendingIntent pendingIntent = PendingIntent.getActivity(context,
uniqueIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remote_view_notification);
remoteViews.setImageViewResource(R.id.remoteview_notification_icon, R.mipmap.ic_navigation_favorites);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_navigation_favorites) //just dummy icon
.setContent(remoteViews) // here we apply our view
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
final Notification notification = notificationBuilder.build();
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification.bigContentView = remoteViews;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(uniqueIntentId, notification);
//don't forget to include picasso to your build.gradle file.
Picasso.with(context)
.load(avatar)
.into(remoteViews, R.id.remoteview_notification_icon, uniqueIntentId, notification);
然後在 layouts 資料夾中定義佈局:
<?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="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<ImageView
android:id="@+id/remoteview_notification_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="2dp"
android:layout_weight="0"
android:scaleType="centerCrop"/>
</LinearLayout>