通知显示空白

时间:2014-05-02 06:54:57

标签: android notifications

在我的通知中,第一个通知显示数据,第二个,第三个,....显示任何内容。它只显示标题和正文是空白的。它应该显示所有通知。这是我的代码。

  NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(context, MainActivity.class);
    // Ensures navigating back to activity, then to Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Create notification.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { // API 16 onwards

        android.app.Notification.Builder builder = new Notification.Builder(context)
        .setContentTitle("Game")
        .setPriority(Notification.PRIORITY_HIGH)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(pendingIntent)

        .setSmallIcon(R.drawable.notification);

         Notification notification = new Notification.InboxStyle(builder)
            .addLine("First message")
            .addLine("Second message")
            .addLine("Thrid message")
            .build();

          notification.flags |= Notification.FLAG_AUTO_CANCEL;

          mNotificationManager.notify(++NOTIF_ID, notification);


    } 

1 个答案:

答案 0 :(得分:0)

见:

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    Notification notification;
    if (Build.VERSION.SDK_INT < 11) {
        notification = new Notification(icon, "Game", when);
        notification.setLatestEventInfo(
                context,
                "Game",
                intent);
    } else {
        notification = new Notification.Builder(context)
                .setContentTitle("Game").setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(intent).setWhen(when).setAutoCancel(true)
                .build();
    }
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;

    notificationManager.notify(++NOTIF_ID, notification);

它会对你有用,因为它对我来说非常有效。