我正在通过集成RemoteViews
来实现自定义推送通知。问题是,远程视图中的按钮没有显示。我没有弄到我做错了什么。
代码:
public class AlarmReceiver extends BroadcastReceiver {
Bitmap bannerimage;
private static int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
bannerimage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.dummyturkey);
MY_NOTIFICATION_ID=1;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.custom_push_layout);
Intent myIntent = new Intent(context,DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
MY_NOTIFICATION_ID,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
System.out.println("Alarm fired AlarmReciever:"+mydate);
remoteViews.setImageViewBitmap(R.id.imgbanner,bannerimage);
Notification myNotification = new Notification.Builder(context)
.setContent(remoteViews)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
XML文件 custom_push_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgbanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This chicken has send you a friend request" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnaccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept" />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnaccept"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>
通知即将发布,但它只显示图像视图,而不显示布局中的按钮或文本视图。
请仅参考上述代码给出解决方案,即。我正在做什么或我错过了什么。请不要发布新的代码。
答案 0 :(得分:2)
使用Notification.Builder(context).setContent
设置通知的常规视图布局。根据{{3}},普通视图布局高度仅限于64dp
。
自定义通知布局的可用高度取决于通知视图。普通视图布局限制为64 dp,扩展视图布局限制为256 dp。
您需要做的是将contentView
和bigContentView
设置为Notification对象。创建两个单独的布局,一个用于法线布局,一个用于大视图布局,并创建两个RemoteViews
。
RemoteViews customViewSmall = new RemoteViews(context.getPackageName(), R.layout.custom_notification_small);
RemoteViews customViewBig = new RemoteViews(context.getPackageName(), R.layout.custom_notification_big);
...
set the values of the views
...
Notification myNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();
myNotification.contentView = customViewSmall;
myNotification.bigContentView = customViewBig;
请注意,bigContentView
可从API16
获取。同样在UI xml中,为所有TextView
视图添加颜色。
修改强>
在v4支持库24中,NotificationBuilderCompat
有新方法setCustomBigContentView()
,因此请将remoteViews
设置为Notification
对象,只需使用NotificationBuilderCompat
。
你可以在这里看到它:Notifications API Guide
结果: