java.lang.IllegalArgumentException: contentIntent required

遇到这个问题:

java.lang.IllegalArgumentException: contentIntent required: pkg=*** id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)

先看懂,好像是没有设置contentIntent导致的.可能有两种情况导致

1.在设置通知时,没有指定PendingIntent里面的intent


PendingIntent pendingIntent = PendingIntent.getActivity(content, requestCode, intent, flags);


如果没有intent的话,可以给一个new Intent(),但不能为null


2.在用NotificationCompat.Builder创建的一个remoteView时,在4.0机器上完美显示了remoteView,但在2.3的机器上显示为空白界面,有时也会报错,看 官方文档:

Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification
layouts.

On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.

For example, action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.

For this reason, you should always ensure that UI controls in a notification are also available in an Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() method.

告诉我们必须用setContentIntent()这个方法创建Notification,但用了之后还是会报错

builder.setContent(remoteViews).setSmallIcon(icon).setTicker(tickerText).setContentIntent(contentIntent);

最后在网上查到这可能是一个bug,必须还要单独为Notification设置contentView

Notification notification = builder.build();

notification.contentView = remoteViews;

问题解决,完整代码如下:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_bar);
remoteViews.setCharSequence(R.id.notification_bar_title_tv, "setText", contentTitle);
NotificationCompat.Builder builder = new Builder(context);
builder.setContent(remoteViews).setSmallIcon(icon)
            .setTicker(tickerText).setContentIntent(contentIntent)
            .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
notification.contentView = remoteViews;
nm.notify(notifyId, notification);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值