您好我在我的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>
提前致谢。
答案 0 :(得分: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 :(得分:1)
@TGMCians谢谢:)
http://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());
答案 2 :(得分:1)
更改布局以使用LinearLayout。遇到相同的问题,这是一个不错的解决方法。