Ciao a tutti.
In un progetto su cui sto lavorando devo mandare delle notifiche FCM (Firebase Cloud Messaging).
Ho seguito quanto descritto qui https://firebase.google.com/docs/clo...oncept-options e ho implementato del codice per preparare il messaggio e per inviarlo via POST.
Funziona tutto a dovere salvo il fatto che se il messaggio contiene dei caratteri speciali (tipo quelli accentati) nella notifica non sono presenti (ad esempio può risulta essere pu)
Di seguito il codice:
Questo metodo invia la POST
Questo metodo crea la notifica FCM e richiama la precedente funziona:codice:public NetworkResponse sendPost(String url, Object postParameters, ArrayList<HeaderProperty> headerProperties) throws ClientProtocolException, IOException { NetworkResponse postResponse = null; CloseableHttpResponse response = null; HttpPost httpPost = new HttpPost(url); if (postParameters != null) { try { httpPost.setEntity(new UrlEncodedFormEntity((ArrayList<NameValuePair>)postParameters)); } catch (Exception e1) { try { JSONObject jsonPostParameter = (JSONObject)postParameters; StringEntity postingString = new StringEntity(jsonPostParameter.toString()); httpPost.setEntity(postingString); } catch (Exception e2) { CommonUtilities.logDebug("sendPost -> formato parametri post non valido: "+e2.toString(), logger); } } } if (headerProperties != null && headerProperties.size() > 0) { for (int i=0; i<headerProperties.size(); i++) { HeaderProperty header = headerProperties.get(i); httpPost.setHeader(header.getHeaderKey(), header.getHeaderValue()); } } HttpClient httpClient = HttpClientBuilder.create().build(); response = (CloseableHttpResponse) httpClient.execute(httpPost); int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); String rsp = EntityUtils.toString(entity); response.close(); postResponse = new NetworkResponse(status, rsp); return postResponse; }
il parametro che contiene il testo con i caratteri speciali è notificationMsgcodice:public static boolean sendFirebaseCloudMessagingNotification(FCMRegistrationVO userToNotify, String notificationMsg, Properties firebaseProperties, NetworkManager networkManager) { try { String serverKey = FirebaseUtils.FIREBASE_CLOUD_MESSAGING_KEY_PREFIX+firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_KEY_PROPERTY); String firebaseCloudMessagingUrl = firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_HTTP_URL_PROPERTY); String firebaseCloudMessagingContentType = firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_CONTENT_TYPE_PROPERTY); ArrayList<HeaderProperty> headerProperties = new ArrayList<HeaderProperty>(); HeaderProperty contentType = new HeaderProperty(NetworkManager.CONTENT_TYPE, firebaseCloudMessagingContentType); HeaderProperty authorization = new HeaderProperty(NetworkManager.AUTHORIZATION, serverKey); headerProperties.add(contentType); headerProperties.add(authorization); JSONObject jsonNotification = new JSONObject(); jsonNotification.put(firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_TITLE_PROPERTY), firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_DEFAULT_TITLE_PROPERTY)); jsonNotification.put(firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_BODY_PROPERTY), notificationMsg); JSONObject jsonBody = new JSONObject(); String to = userToNotify.getFcmRegistrationToken(); jsonBody.put(firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_TO_PROPERTY), to); jsonBody.put(firebaseProperties.getProperty(FirebaseUtils.FIREBASE_CLOUD_MESSAGING_NOTIFICATION_PROPERTY), jsonNotification); NetworkResponse rsp = networkManager.sendPost(firebaseCloudMessagingUrl, jsonBody, headerProperties); if (rsp.getResponseStatus() != NetworkManager.NETWORK_RESPONSE_OK) return false; } catch (Exception e) { return false; } return true; }
Qualcuno ha idea di come fare per inviare i caratteri speciali nella notifica?
Ciao e grazie

Rispondi quotando