7

次のコードを使用してプッシュ通知を送信しようとしています:

    Message message = new Message.Builder().addData("appName", appData.name)
.addData("message", pushData.message).build();

受信側のコードは次のとおりです。

String message = intent.getStringExtra("message");

メッセージが英語のラテン文字セットの場合、すべてが機能します。しかし、他の言語や文字 ç を試すと、疑問符として表示されるか、文字列から削除されます。

注: utf-8 でエンコードされています

4

5 に答える 5

9

Java サーバー

Message messagePush = new Message.Builder().addData("message", URLEncoder.encode("your message éèçà", "UTF-8")))

Android アプリケーション

String message = URLDecoder.decode(intent.getStringExtra("message"), "UTF-8");
于 2013-04-03T14:15:47.923 に答える
2

gcm-server ライブラリにも同様の問題がありました。私の回避策は、カスタム送信者を使用してメソッドをオーバーライドし、呼び出しpostでエンコードとして UTF8 を使用することでした。getBytes()Google App Engine で動作します。

カスタム送信者クラスのコード:

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.logging.Level;

import com.google.android.gcm.server.Sender;

/**
 * Workaround to avoid issue #13 of gcm-server
 * @see https://code.google.com/p/gcm/issues/detail?id=13&q=encoding
 * 
 * @author rbarriuso /at/ tribalyte.com
 *
 */
public class Utf8Sender extends Sender {

    private final String key;

    public Utf8Sender(String key) {
        super(key);
        this.key = key;
    }

    @Override
    protected HttpURLConnection post(String url, String contentType, String body) throws IOException {
        if (url == null || body == null) {
            throw new IllegalArgumentException("arguments cannot be null");
        }
        if (!url.startsWith("https://")) {
            logger.warning("URL does not use https: " + url);
        }
        logger.fine("Utf8Sender Sending POST to " + url);
        logger.finest("POST body: " + body);
        byte[] bytes = body.getBytes(UTF8);
        HttpURLConnection conn = getConnection(url);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", contentType);
        conn.setRequestProperty("Authorization", "key=" + key);
        OutputStream out = conn.getOutputStream();
        try {
            out.write(bytes);
        } finally {
            close(out);
        }
        return conn;
    }

    private static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // ignore error
                logger.log(Level.FINEST, "IOException closing stream", e);
            }
        }
    }

}
于 2014-03-27T12:36:14.990 に答える
1

これらはいくつかの優れた解決策でしたが、トピック メッセージングを使用して通知を送信していたため、役に立ちませんでした。この通り

HTTP ヘッダーには、次のヘッダーが含まれている必要があります。Authorization: key=YOUR_API_KEY Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=プレーン テキストの場合は UTF-8。Content-Type を省略した場合、形式はプレーン テキストと見なされます。

ただし、私はクラウド エンドポイントを使用しており、私のアプリ (既に出回っています) は json を想定しているため、リクエストをプレーン テキストでフォーマットすることはできませんでした。解決策は、上記のドキュメントを無視し、バックエンドで http 要求ヘッダーを次のようにフォーマットすることでした。

 connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

魔法のように、突然すべての特殊文字 (読み: 日本語) が、フロント エンドの変更なしでアプリケーションに渡されます。私の完全なhttpポストコードは以下です( where payload = Cloud endpoint model object, converted to json<String> via Gson):

 try {
        URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        connection.setRequestProperty("Authorization", "key=" + API_KEY);
        connection.setRequestMethod("POST");

        byte[] bytes=payload.getBytes("UTF-8");
        OutputStream out = connection.getOutputStream();
        try {
            out.write(bytes);
        } finally {
            out.close();
        }

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            // OK
            log.warning("OK");
        } else {
            // Server returned HTTP error code.
            log.warning("some error "+connection.getResponseCode());
        }
    } catch (MalformedURLException e) {
        // ...
    }
于 2015-08-18T06:50:39.863 に答える
0

デバッガーを中断し、送信していると思われるメッセージのバイトを表示します

于 2012-11-07T08:43:13.957 に答える