我尝试使用以下代码显示自定义推送通知:
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 :(得分: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);
}
}
希望这有帮助。
答案 1 :(得分: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
);
答案 2 :(得分: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>