问题
I do not know how to use Notification Inbox style exactly. I just tried this below code and it is showing error at Notification.InboxStyle(). What mistake I did? Can anyone help me out with this issue? Here is my code..
private void generateNotification(Context context, String message) {
System.out.println(message+"++++++++++2");
int icon = R.drawable.ic_message;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(R.string.message_title);
String subTitle = context.getString(R.string.message_subtitle);
Intent notificationIntent = new Intent(context, Output.class);
notificationIntent.putExtra("content", message);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification base = new Notification.Builder(context)
.setTicker(message)
.setSmallIcon(icon)
.setWhen(when)
.setContentTitle(title)
.setContentText(subTitle)
.setNumber(4)
.setContentIntent(intent)
.build();
Notification notification = new Notification.InboxStyle(base)
.addLine("First Message")
.addLine("Second Message")
.addLine("Third Message")
.addLine("Fourth Message")
.setBigContentTitle("Here Your Messages")
.setSummaryText("+3 more")
.build();
//To play the default sound with your notification:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
Thanks in advance.
回答1:
The constructor of is
Notification.InboxStyle(Notification.Builder builder)
and you are passing in a Notification. Do like this instead
Notification notification = new Notification.InboxStyle(new Notification.Builder(context)
.setTicker(message)
.setSmallIcon(icon)
.setWhen(when)
.setContentTitle(title)
.setContentText(subTitle)
.setNumber(4)
.setContentIntent(intent))
.addLine("First Message")
.addLine("Second Message")
.addLine("Third Message")
.addLine("Fourth Message")
.setBigContentTitle("Here Your Messages")
.setSummaryText("+3 more")
.build();
回答2:
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message,
String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
if (G.appendNotificationMessages) {
// store the notification in shared pref first
mPreferenceManager.addNotification(message);
// get the notifications from shared preferences
String oldNotification = mPreferenceManager.getNotifications();
List<String> messages = Arrays.asList(oldNotification.split("\\|"));
Log.i(TAG,"showSmallNotification "+ messages);
for (int i = messages.size() - 1; i >= 0; i--) {
inboxStyle.addLine(messages.get(i));
}
} else {
Log.i(TAG,"showSmallNotification-n "+ message);
inboxStyle.addLine(message);
}
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0).setAutoCancel(true)
.setContentTitle(title).setContentIntent(resultPendingIntent).setSound(alarmSound).setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp)).setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon)).setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(G.NOTIFICATION_ID, notification);
}
来源:https://stackoverflow.com/questions/20681483/how-to-use-notification-inbox-style-using-android