31

拡張可能な通知を実装しようとしましたが、そのためにInboxStyleを使用しました。

ドキュメントからの次の画像に基づく:

受信トレイスタイルの通知

テキストのスタイルを設定できるはずです。この場合、「GooglePlay」を太字にします。

InboxStyleにはaddLine()、CharSequenceを渡すことができる場所しかありません。いくつかのhtmlフォーマットを試してHtml.fromHtml()使用しましたが、成功しませんでした。

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("title");
inboxStyle.setSummaryText("summarytext");
// fetch push messages
synchronized (mPushMessages) {
    HashMap<String, PushMessage> messages = mPushMessages.get(key);
    if (messages != null) {
        for (Entry<String, PushMessage> msg : messages.entrySet()) {
            inboxStyle.addLine(Html.fromHtml("at least <b>one word</b> should be bold!");
        }
        builder.setStyle(inboxStyle);
        builder.setNumber(messages.size());
    }
}

これについて何か考えはありますか?

4

5 に答える 5

49

を使用する必要はありませんfromHtml。私はfromHtml過去に問題を抱えていました(表示するものがユーザーからのものである場合、コードインジェクションは醜いものになる可能性があります)。また、フォーマット要素を入れるのは好きではありませんstrings.xml(翻訳にサービスを使用すると、HTMLタグが台無しになる可能性があります)。

このメソッドは、通知( 、、など)にaddLineテキストを設定するほとんどのメソッドと同様に、パラメーターとしてを取ります。setTickersetContentInfosetContentTitleCharSequence

したがって、を渡すことができますSpannable。「これを太字で斜体にします。」としましょう。次のようにフォーマットできます(もちろん、位置をハードコードしないでください)。

Spannable sb = new SpannableString("Bold this and italic that.");
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 14, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);

「今日は[DAY] 、おはようございます!」のように、ローカライズされた文字列を使用して動的に文字列を作成する必要がある場合は、プレースホルダーを使用して文字列を次の場所に配置しますstrings.xml

<string name="notification_line_format">Today is %1$s, good morning!</string>

次に、次のようにフォーマットします。

String today = "Sunday";
String lineFormat = context.getString(R.string.notification_line_format);
int lineParamStartPos = lineFormat.indexOf("%1$s");
if (lineParamStartPos < 0) {
  throw new InvalidParameterException("Something's wrong with your string! LINT could have caught that.");
}
String lineFormatted = context.getString(R.string.notification_line_format, today);

Spannable sb = new SpannableString(lineFormatted);
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), lineParamStartPos, lineParamStartPos + today.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inboxStyle.addLine(sb);

「今日は日曜日、おはようございます!」と表示され、私が知る限り、Androidのすべてのバージョンで動作します。

于 2014-02-09T16:03:51.820 に答える
7

あなたのコードは、Android4.1.1を搭載したSamsungS3で魅力的なように機能しました。使用しているAndroidのバージョンは何ですか?

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.view.Menu;

public class MainActivity extends Activity {

    private final int ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.addLine(Html
                .fromHtml("<i>italic</i> <b>bold</b> text works"));
        mBuilder.setStyle(inboxStyle);
        mBuilder.setNumber(1);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(ID, mBuilder.build());
    }

}
于 2013-02-19T13:07:57.720 に答える
7

私はこれのためにヘルパーメソッドを作りました:

private final StyleSpan mBoldSpan = new StyleSpan(Typeface.BOLD);

private SpannableString makeNotificationLine(String title, String text) {
    final SpannableString spannableString;
    if (title != null && title.length() > 0) {
        spannableString = new SpannableString(String.format("%s  %s", title, text));
        spannableString.setSpan(mBoldSpan, 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        spannableString = new SpannableString(text);
    }
    return spannableString;
}

このように使用します:

inboxStyle.addLine(makeNotificationLine("UserXYZ", "How are you?"));
于 2014-08-25T12:35:21.983 に答える
3

これが最も簡単な解決策です

InboxStyle私の場合は必要なかったので使用しません


CharSequence boldUsernameMessage = Html.fromHtml("<b>@" + username +"</b> " + message);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
    .setContentText(boldUsernameMessage)
    .setStyle(new NotificationCompat.BigTextStyle().bigText(boldUsernameMessage)) // Makes it expandable to show the message
    .build();

結果

ここに画像の説明を入力してください

于 2016-04-26T18:37:45.807 に答える
2

太字のテキストには<strong>タグを使用する必要があります。コードに問題はありません。に変更<b>するだけ<strong>です。

編集:コードを次のように変更する必要があります:

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("title");
inboxStyle.setSummaryText("summarytext");
// fetch push messages
synchronized (mPushMessages) {
    HashMap<String, PushMessage> messages = mPushMessages.get(key);
    if (messages != null) {
        for (Entry<String, PushMessage> msg : messages.entrySet()) {
            inboxStyle.addLine(Html.fromHtml("at least <strong>one word</strong> should be bold!");
        }
        builder.setStyle(inboxStyle);
        builder.setNumber(messages.size());
    }
}
于 2014-03-14T20:00:02.043 に答える