我正在Android中开发Actionable Notifications。
void createActionableNotification() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
// Actions are just fake
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.moneybag)
.setContentIntent(pIntent)
.addAction(R.drawable.moneybag, "Call", pIntent)
.addAction(R.drawable.moneybag, "More", pIntent)
.addAction(R.drawable.moneybag, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
该应用程序使用目标SDK运行,Compile SDK API为24,Min SDK为20。 我用Marshmallow Device进行了测试。但在模拟器中工作正常。仿真器SDK版本为24
答案 0 :(得分:0)
试试这个:适用于api> = 16
Notification notification = new Notification.Builder(myContext)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.moneybag)
.setContentIntent(pIntent)
.setPriority(Notification.PRIORITY_MAX)
.addAction(R.drawable.moneybag, "Call", pIntent)
.addAction(R.drawable.moneybag, "More", pIntent)
.addAction(R.drawable.moneybag, "And more", pIntent).build();
//The rest of your options
.build();
如果列表中存在任何正在进行的Notification
(例如媒体播放器控件),或者您通过USB将电话连接到PC或正在编辑文本时,按钮将不会出现。
您可以尝试Notification.PRIORITY_MAX
来解决此问题,或PRIORITY_HIGH