lulu666 lulu666 - 2 months ago 9x
Android Question

Push Notifications Android, encoding and decoding

When I receive notifications in my device I can't avoid seeing weird characters, kind of: í©.


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
} catch (UnsupportedEncodingException e) {

In the server side I do the encoding:

$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');

What am I doing wrong? Thanks


In my case, I do not encode nor decode any text when I'm sending the push and it looks good on the device.

PHP Code (to send the Push)

    // 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, '' );
    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 code to retrieve the push and create notification

public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());

private void sendNotification(String title, String messageBody) {
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

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



enter image description here