0

您好,我在我的android應用程序中顯示自定義通知。但問題是沒有顯示自定義通知。我看到很多線索,但沒有找到與我的問題有關的任何答案。自定義通知未在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> 

感謝。

回答

1

@TGMCians謝謝:)

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

可能使用支持庫,遠程視圖的概念並不在安卓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

狀態欄通知要求所有如下:

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 

不指定一個圖標,這意味着你的通知是無效的。

+0

我添加了所有(圖標,標題,消息,PendingIntent),我的圖標也有效 –

+0

您的解決方案對我無效 –