对于kitkat

时间:2015-10-21 07:09:25

标签: android android-notifications

          NotificationCompat.Builder b = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_notify_small) // Set the small icon to use in the notification layouts.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_notify_medium_ridlr))
                .setContentTitle(notif.size() + " new notifications") //Set the title (first row) of the notification, in a standard notification.
                .setContentText("Touch to view");

        b.setContentIntent(contentIntent);

        NotificationCompat.InboxStyle inboxStyle =  new NotificationCompat.InboxStyle();
        try {
            for (Notification n : notif) {
                String title = "<b>"+n.extras.getString("android.title")+"</b>" +" "+ n.extras.getString("android.text");
                inboxStyle.addLine(Html.fromHtml(title));
            }
        } catch (Exception e) {
            Crittercism.leaveBreadcrumb(TAG + " Crash due to some internal error from GCM.");
            e.printStackTrace();
        }

        b.setStyle(inboxStyle);
        Notification n1 = b.build();
        n1.flags |= Notification.FLAG_AUTO_CANCEL;
        n1.defaults |= Notification.DEFAULT_SOUND;
        n1.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.cancel(notificationNumber - 1); // removing previous notification
        mNotificationManager.notify(notificationNumber, n1);

        notificationReceivedListener();

此代码在版本4.4及更高版本上完美运行。 4.4版本以下的notification.extras替代方案

1 个答案:

答案 0 :(得分:0)

你需要使用:notification.setLatestEventInfo(this,getText(R.string.new_friend_request_exist),         “你有新朋友的要求”,         contentIntent);