Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2007
    Messaggi
    331

    Invio di caratteri speciali in una POST

    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
    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;
    	}
    Questo metodo crea la notifica FCM e richiama la precedente funziona:
    codice:
    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;
    	}
    il parametro che contiene il testo con i caratteri speciali è notificationMsg

    Qualcuno ha idea di come fare per inviare i caratteri speciali nella notifica?

    Ciao e grazie

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    hai già provato a fare un encode / decode?
    Qui:

    http://stackoverflow.com/questions/4...haracters-like

    suggeriscono un Base64 come soluzione.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.