关于Android Notification.Builder不显示通知的问题

今天看Android书上关于Notification的代码

  <span style="white-space:pre">	</span>Intent i = new Intent(this,NotificationActivity.class);
        i.putExtra("notificationID",notificationID);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,0);

        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Notification notif = new Notification(
                R.mipmap.ic_launcher,
                "Reminder:Meeting starts in 5 minutes",
                System.currentTimeMillis());

        CharSequence from = "System Alarm";
        CharSequence message = "Meeting with customer at 3pm...";
        notif.setLatestEventInfo(this,from,message,pendingIntent);
  <span style="white-space:pre">	</span>notif.vibrate = new long[]{100,250,100,500};
        nm.notify(notificationID,notif);
功能成功实现了。但是发现Notification的构造函数和setLatestEventInfo函数已经不推荐使用了,官方建议使用Notification.Builder来代替。

参照文档,我这样写了。

 Notification notif = new Notification.Builder(this)
                .setContentIntent(pendingIntent)
                .setTicker("Reminder:Meeting starts in 5 minutes")
                .setContentTitle("System Alarm")
                .setContentText("Meeting with customer at 3pm...")
                .build();
发现并没有在通知栏中显示通知。

后来在 这里 找到了这样的话:

A Notification object must contain the following:

A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()


所以我修改成这样:

 Notification notif = new Notification.Builder(this)
                .setContentIntent(pendingIntent)
                .setTicker("Reminder:Meeting starts in 5 minutes")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("System Alarm")
                .setContentText("Meeting with customer at 3pm...")
                .build();


难道必须三者都指定才能显示通知吗?我进行了尝试,如果不调用
setContentTitle() 或 setContentText()
是不会有问题的。


但是,不调用

setTicker("Reminder:Meeting starts in 5 minutes")
会造成通知栏不显示非展开模式的通知,只显示展开模式下的通知。


如果不调用 

<strong>setSmallIcon(R.mipmap.ic_launcher)</strong>
一定不显示通知,包括展开模式下的。


上图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值