【问题标题】:Push Notifications Android, encoding and decoding推送通知 Android,编码和解码
【发布时间】:2016-08-22 14:52:32
【问题描述】:

当我在设备中收到通知时,我无法避免看到奇怪的字符,例如:é。

GcmListenerService.class

public void onMessageReceived(String from, Bundle data) {

    String tit = data.getString("title");
    String message = data.getString("body");

    String title = null;
    try {
        title= URLDecoder.decode(tit, "UTF-8"); // tit and title have the same output
        sendNotification(title,message)
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

在服务器端我进行编码:

$payload = array('body' => utf8_encode($texto),
                 'title' => utf8_encode($name),
                 'Room' => utf8_encode($texto));

$data = array(
          'data' => $payload,
          'to' => $tokenDevice);

$headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json; charset=utf-8');

我做错了什么?谢谢

【问题讨论】:

  • 编码后服务器端的字符串如何?

标签: android encoding utf-8 push-notification


【解决方案1】:

在我的情况下,我在发送推送时不编码也不解码任何文本,它在设备上看起来不错。

PHP 代码(发送推送)

    // API access key from Google API's Console
    define( 'API_ACCESS_KEY', $APIKEY );
    $registrationIds = $tokens;

    // prep the bundle
    $msg = array
    (
        'message'   => $message,
        'title'     => $title
    );

    $fields = array
    (
        'registration_ids'  => $registrationIds,
        'data'          => $msg
    );

    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $res = curl_exec($ch);
    $result = json_encode($res);

    echo $result;

用于检索推送和创建通知的 Android 代码

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());
    sendNotification(remoteMessage.getData().get("title"),
       remoteMessage.getData().get("message"));
}

private void sendNotification(String title, String messageBody) {
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.white_notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.logo))
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

结果

【讨论】:

    【解决方案2】:

    我们在 Android 中有 Uri 类,它可以帮助我们解码文本以接受 android 中的所有格式。

    String  title = Uri.encode(tit, "UTF-8");
    

    //--------------------------------------------- ------------

    /**
         * Encodes characters in the given string as '%'-escaped octets
         * using the UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers
         * ("0-9"), and unreserved characters ("_-!.~'()*") intact. Encodes
         * all other characters with the exception of those specified in the
         * allow argument.
         *
         * @param s string to encode
         * @param allow set of additional characters to allow in the encoded form,
         *  null if no characters should be skipped
         * @return an encoded version of s suitable for use as a URI component,
         *  or null if s is null
         */
    

    并将消息解码为字符串:

      String message = Uri.decode(encodedMessage);
    

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多