如何使用android的Notification Inbox风格?

时间:2013-12-19 12:11:49

标签: android notifications styles inbox

我不知道如何准确使用通知收件箱样式。我刚刚尝试了下面的代码,它在Notification.InboxStyle()中显示错误。我做错了什么?任何人都可以帮我解决这个问题吗?这是我的代码..

  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);



}

提前致谢。

2 个答案:

答案 0 :(得分:6)

构造函数是

Notification.InboxStyle(Notification.Builder builder)

你正在传递通知。 这样做

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();

答案 1 :(得分:0)

 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);
}