【问题标题】:Custom Notification is not displaying in Android自定义通知未在 Android 中显示
【发布时间】:2014-05-24 16:13:28
【问题描述】:

您好,我正在我的 android 应用程序中显示自定义通知。但问题是没有显示自定义通知。我看到了很多帖子,但没有找到与我的问题相关的任何答案。

我找不到问题出在哪里。你能看看这个吗?

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// display the notification
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.notificationIcon, Util.notificationBitmap);
contentView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
contentView.setTextViewText(R.id.notificationContent, notificationMessage);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_logo)
                    .setContentTitle(Util.notificationTitle)
                    .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(notificationMessage))
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setContentText(notificationMessage);

mBuilder.setContent(contentView);
// build notification
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify((int)SystemClock.currentThreadTimeMillis(), mBuilder.build());

custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/notificationIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

    <TextView
        android:id="@+id/notificationTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notificationIcon"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/notificationContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/notificationTitle"
        android:layout_toRightOf="@id/notificationIcon"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

提前致谢。

【问题讨论】:

    标签: android android-notifications remoteview android-remoteview


    【解决方案1】:

    更改布局以使用 LinearLayout。有同样的问题,这是一个很好的解决方法。

    【讨论】:

    【解决方案2】:

    状态栏通知需要以下所有条件:

    An icon for the status bar
    A title and message, unless you define a custom notification layout
    A PendingIntent, to be fired when the notification is selected
    from Status Bar Notifications - Creating Notifications 
    

    您没有指定图标,这意味着您的通知无效。

    【讨论】:

    • 我添加了所有(图标、标题、消息、PendingIntent)并且我的图标也有效
    【解决方案3】:

    @TGMCians 谢谢 :)

    https://chat.stackoverflow.com/transcript/15?m=16698842#16698842

    如果您正在寻找自定义通知,可能使用支持库,远程视图概念在 Android 2.X 中不起作用。

    这对我有用并解决了我的问题。

    // display the notification
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    
    // Android 2.x does not support remote view + custom notification concept using 
    // support library
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        mBuilder.setSmallIcon(R.drawable.ic_stat_logo);
        mBuilder.setContentTitle(Util.notificationTitle)
                .setStyle(
                        new NotificationCompat.BigTextStyle()
                                .bigText(notificationMessage))
                .setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
                .setLights(Color.WHITE, 500, 500)
                .setContentText(notificationMessage);
    } else {
        RemoteViews customNotificationView = new RemoteViews(getPackageName(),
                R.layout.custom_notification);
    
        customNotificationView.setImageViewBitmap(R.id.notificationIcon, Util.notificationBitmap);
        customNotificationView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
        customNotificationView.setTextViewText(R.id.notificationContent, notificationMessage);
    
        mBuilder.setContent(customNotificationView);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setLights(Color.WHITE, 500, 500);
    }
    // build notification
    mBuilder.setContentIntent(pendingIntent);
    mNotificationManager.notify(1000, mBuilder.build());
    

    【讨论】:

      【解决方案4】:

      只需确保您的自定义通知设计简单并包含一个父布局-RelativeLayout/LinearLayout

      示例布局

      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <ImageView
              android:id="@+id/notificationIcon"
              android:layout_width="40dp"
              android:layout_height="40dp"
              android:layout_centerVertical="true"
              />
      
          <TextView
              android:id="@+id/notificationTitle"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="@dimen/padding_10dp"
              android:layout_marginTop="@dimen/margin_8dp"
              android:layout_marginEnd="@dimen/margin_8dp"
              android:textSize="13sp"
              android:layout_toEndOf="@+id/notificationIcon"
              android:layout_toRightOf="@id/notificationIcon"
              android:maxLines="1"
              android:text="jhyfjhuf"
              android:textStyle="bold" />
      
          <TextView
              android:id="@+id/notificationContent"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/notificationTitle"
              android:layout_marginStart="@dimen/padding_10dp"
              android:layout_marginTop="@dimen/margin_4dp"
              android:layout_marginBottom="@dimen/margin_8dp"
              android:layout_marginEnd="@dimen/margin_8dp"
              android:layout_toRightOf="@id/notificationIcon"
              android:textSize="11sp"
              android:maxLines="1"
              android:text="Notification Description" />
      
      </RelativeLayout>
      

      并在notification builder中设置此视图:

      builder.setCustomContentView(customContentView)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 2018-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多