2013-10-29 174 views
0

我嘗試顯示自定義推送通知與下面的代碼:自定義推送通知不顯示

RemoteViews contentView = new RemoteViews(app.context.getPackageName(), R.layout.multiline_notification_layout); 
       contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
       contentView.setTextViewText(R.id.text, text); 
       contentView.setTextViewText(R.id.title, title); 

      notification = new NotificationCompat.Builder(app.context) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setWhen(0) 
        .setContent(contentView) 
        .setContentIntent(contentIntent) 
        .setAutoCancel(true) 
        .build(); 
     } 

     mNotificationManager.notify(NOTIFICATION_ID, notification); 
     NOTIFICATION_ID++; 

,我瞭解當推收到的圖標。 如果我將刪除.setSmallIcon(R.drawable.ic_launcher)我不會顯示。

佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_marginRight="10dp" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="5dp" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Title" 
    android:id="@+id/title" 
    android:layout_toRightOf="@+id/image" 
    android:layout_margin="5dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Text" 
    android:id="@+id/text" 
    android:layout_below="@+id/title" 
    android:layout_toRightOf="@+id/image" 
    android:layout_margin="5dp" 
    android:inputType="textMultiLine" /> 

回答

0

試試這個:

private void generateNotification(Context context, String message) { 

int icon = R.drawable.ic_launcher; 
long when = System.currentTimeMillis(); 
String appname = context.getResources().getString(R.string.app_name); 
NotificationManager notificationManager = (NotificationManager) context 
     .getSystemService(Context.NOTIFICATION_SERVICE); 
int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
Notification notification; 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
     new Intent(context, myactivity.class), 0); 

// To support 2.3 os, we use "Notification" class and 3.0+ os will use 
// "NotificationCompat.Builder" class. 
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { 
    notification = new Notification(icon, message, 0); 
    notification.setLatestEventInfo(context, appname, message, 
      contentIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 

} else { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context); 
    notification = builder.setContentIntent(contentIntent) 
      .setSmallIcon(icon).setTicker(appname).setWhen(0) 
      .setAutoCancel(true).setContentTitle(appname) 
      .setContentText(message).build(); 

    notificationManager.notify((int) when , notification); 

} 

}

希望這有助於。

+0

確切地說,這不是我所需要的。用「簡單」推送通知我有問題。但是,我有自定義佈局的推送通知問題。 –

0

我不確定爲什麼你的代碼不起作用,但下面的代碼適用於我用自定義佈局顯示通知。唯一的(輕微的)問題是它使用了不推薦的方法。

 NotificationManager 
      mManager   = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent myIntent = new Intent(this,MyActivity.class); 
     Notification 
      notification  = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis()); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     RemoteViews 
      contentView  = new RemoteViews(getPackageName(), R.layout.notification); 
     contentView.setImageViewResource(
      R.id.image, 
      R.drawable.notification_icon 
          ); 
     contentView.setTextViewText (
      R.id.title, 
      title 
          ); 
     contentView.setTextViewText (
      R.id.text, 
      message 
          ); 
     notification.contentView 
          = contentView; 
     notification.contentIntent 
          = PendingIntent.getActivity (
      this.getBaseContext(), 
      0, 
      myIntent, 
      PendingIntent.FLAG_CANCEL_CURRENT 
          ); 
     mManager.notify(
      0, 
      notification 
          ); 
0

該代碼與我一起工作,我希望能與您

String ns = Context.NOTIFICATION_SERVICE; 
    notificationManager = (NotificationManager) getSystemService(ns); 

    notification = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.home) 
      .setContentTitle("My notification") 
      .setContentText("Notification!") 
      .setOngoing(true) /** notification will appear as ongoing notification */ 
      .build(); 

    /** set a custom layout to the notification in notification drawer */ 
    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.multiline_notification_layout); 
    notification.contentView = notificationView; 

    Intent intent = new Intent(getBaseContext(), C1.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 1, intent, 0); 
    notificationView.setOnClickPendingIntent(R.id.image, pendingIntent); 

    notificationManager.notify(1, notification); 

工作這是C1類

public class C1 extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    /** write here what you want to happen if user clicked on the image view */ 

    /** finish this activity */ 
    finish(); 
} 
} 

的代碼,你必須如下定義C1類在艙單

<activity 
     android:name=".C1" 
     android:label="@string/app_name" 
     android:excludeFromRecents="true" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
     </intent-filter> 
    </activity> 
+0

感謝您的代碼。我無法嘗試,因爲我試圖做的項目已經忘了。但我希望這對別人有幫助! –