自定义通知在Android中不显示

问题描述 投票:1回答:3

你好,我在我的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
3个回答
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 

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


1
投票

@TGMCians 谢谢:)

http:/chat.stackoverflow.comtranscript15?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());

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.