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++;

当 push 收到它的图标时,我看到的只是。如果我要删除 .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" />

4

3 回答 3

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
                             );
于 2013-10-29T14:34:40.493 回答
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>
于 2014-03-29T15:06:36.127 回答
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);

}

}

希望这可以帮助。

于 2013-10-29T10:57:19.307 回答