Android-多行通知无效

时间:2015-07-09 09:13:26

标签: java android

首先,我阅读this并使用此简单代码进行多次通知:

    public static void notifyMessage()
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
     int notifyID = 1;
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    int numMessages = 0;
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker")
            .setContentText("Events received")
            .setNumber(++numMessages)
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig"));
    Notification notification = builder.build();
    notificationManager.notify(1, notification);

它在工作。如果消息的长度大于通常的限制,它将显示" bigbig ... "。
在下一步中,我想要使用用户&#39 ; s要在通知中显示的消息。所以我将功能改为:

      public static void notifyMessage(String message , boolean flashLed)
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
     int notifyID = 1;
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    int numMessages = 0;
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker")
            .setContentText(message)
            .setNumber(++numMessages)
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(message));
    if (flashLed)
    {
        builder.setLights(0xFFFF0000, 500, 500);
    }
    Notification notification = builder.build();
    notificationManager.notify(1, notification);
}

我尝试显示消息而不是" bigbigbig .... "但它总是向我显示简短的通知类型。我也试过这个:

  public static void notifyMessage(String message)
    {
        NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

     NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
     NotificationCompat.InboxStyle inboxStyle =
                    new NotificationCompat.InboxStyle();
     String[] events = new String[4];
            // Sets a title for the Inbox in expanded layout
     events[0] = message.substring(0, 15);
     events[1] = message.substring(15, 35);
     events[2] = message.substring(35, 55);
     events[3] = message.substring(55, 75);
     inboxStyle.setBigContentTitle("Event tracker details:");

      // Moves events into the expanded layout
    for (int i=0; i < events.length; i++) {

       inboxStyle.addLine(events[i]);
    }
    builder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Event tracker"")
            .setContentText("Events received")
            .setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_MAX)
            .setStyle(inboxStyle);

    Notification notification = builder.build();
    notificationManager.notify(1, notification);
}

但是新邮件到达时应用程序崩溃了。如何改变第二或第三功能?

5 个答案:

答案 0 :(得分:0)

.setContentText("")

中删除内容

而是添加:

.setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message));

示例:

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(GetNotificationIcon())
                        .setContentTitle(Constants.APP_NAME)
                        .setContentText("")
                        .setSound(uri)
                        .setVibrate(vibrate)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message));

答案 1 :(得分:-1)

请参阅此链接 - https://developer.android.com/guide/topics/ui/notifiers/notifications.html

    NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
      inboxStyle.setBigContentTitle("bigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbigbig");
   // Moves the expanded layout object into the notification object.
      builder.setStyle(inBoxStyle);

答案 2 :(得分:-1)

不是直接将样式设置到构建器,而是将文本消息添加到样式中,然后将BigTextStyle添加到构建器。 尝试替换以下行

setStyle(new NotificationCompat.BigTextStyle()

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        bigText.bigText("Your long message");
        builder.setStyle(bigText);

答案 3 :(得分:-1)

我已经检查过Last方法对我来说运行正常。确保您使用的是更新的支持V4 库。

您可以从here下载。

在libs文件夹中复制库并执行清理&amp; 构建项目,然后运行应用程序。

希望它会对你有所帮助。

答案 4 :(得分:-1)

以下代码适用于我。

 NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)

                .setContentTitle("title")
                .setContentText("message")
                .setLargeIcon(bitmap)
                .setSmallIcon(R.drawable.ic_notif)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setAutoCancel(true);

        builder.setContentIntent(pendingIntent);
        manager.notify(requestID, builder.build())