0

プッシュ通知用のカスタム UI が必要です。そのために、RemoteViews を使用しています。通知を作成するためのコード:

 RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
    RemoteViews expandedNotificationView = new RemoteViews(context.getPackageName(), R.layout.expanded_custom_notification);
    notificationView.setTextViewText(R.id.notification_title, messagesToBeShown);
    expandedNotificationView.setTextViewText(R.id.expanded_notification_title, "Message Received in expanded ");
    expandedNotificationView.setTextViewText(R.id.expanded_notification_message, messagesToBeShown);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setContentIntent(pendingIntent);
    builder.setPriority(Notification.PRIORITY_MAX);
    Notification notification = builder.build();
    notification.contentView = notificationView;
   //        Notification notification = new             NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
   //                .setContentIntent(pendingIntent).setContent(notificationView)
   //                .setAutoCancel(true).build();
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        notification.bigContentView = expandedNotificationView;
    }
    notification.flags |= Notification.DEFAULT_LIGHTS |      Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(R.string.notification_number, notification);

カスタム レイアウト用の XML ファイル:

Expanded_custom_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="match_parent"
android:orientation="horizontal">

<ImageView
    android:id="@+id/expanded_notifiation_image"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<LinearLayout
    android:layout_width="80dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/expanded_notification_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />

    <TextView
        android:id="@+id/expanded_notification_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />
</LinearLayout>

custom_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="match_parent"
android:orientation="horizontal"
android:weightSum="100">

<ImageView
    android:id="@+id/notifiation_image"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="20"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/notification_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#fff" />

<Button
    android:id="@+id/mark_task_complete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="40"
    android:text="mark as complete" />

</LinearLayout>

このコードの問題は次のとおりです。

  • スワイプで拡大しない
  • 動作が定義されていない場合があり、bigContentView が表示されることがあります。

私を助けてください。前もって感謝します。

4

1 に答える 1