1

您好,我正在我的 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>

提前致谢。

4

4 回答 4

5

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

于 2020-05-07T17:00:57.503 回答
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 

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

于 2014-05-24T16:39:51.137 回答
1

@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());
于 2014-05-25T16:37:55.827 回答
0

只需确保您的自定义通知设计简单并包含一个父布局 -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)
于 2021-10-11T04:57:54.723 回答