我正在使用此代码进行自定义通知。问题是字符串中的整个文本是单行的。其次,没有可见的操作按钮。
private void showNotification(String msg){
//Creating a notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_alert);
builder.setContentTitle("Alert");
builder.setContentText(msg);
builder.setAutoCancel(false);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
//builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.addAction(R.drawable.ic_call, "Accept", pendingIntent).build();
// builder.addAction(R.drawable.ic_alert, "Reject", pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
答案 0 :(得分:0)
将BigTextStyle用于包含多行文字的通知,并将NotificationCompat.Action用于操作按钮。
private void showNotification(String msg) {
//Creating a notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_alert);
builder.setContentTitle("Alert");
builder.setContentText(msg);
builder.setAutoCancel(false);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.addAction(R.drawable.ic_call, "Accept", pendingIntent);
// builder.addAction(R.drawable.ic_alert, "Reject", pendingIntent);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}