Android中的百度推送通知

时间:2017-01-06 06:12:14

标签: android push-notification baidu

如何从百度推送通知中的键值对中检索数据?

Google推送通知中GcmListenerService中的Games.getGamesServerAuthCode(gameHelper.getApiClient(), [web_client_id]).setResultCallback(new ResultCallback<Games.GetServerAuthCodeResult>() { @Override public void onResult(@NonNull Games.GetServerAuthCodeResult result) { if (result.getStatus().isSuccess()) { String authCode = result.getCode(); exchangeAuthCodeForToken(authCode); } } }); 方法中的class AuthToken { String access_token; String token_type; int expires_in; String id_token; } void exchangeAuthCodeForToken(final String authCode) { AsyncTask<Void, Void, AuthToken> task = new AsyncTask<Void, Void, AuthToken>() { @Override protected AuthToken doInBackground(Void... voids) { try { URL url = new URL("https://www.googleapis.com/oauth2/v4/token"); Map<String, Object> params = new LinkedHashMap<>(); params.put("code", authCode); params.put("client_id", "[web_client_id]"); params.put("client_secret", "[secret]"); params.put("redirect_uri", "[redirect_uri]"); params.put("grant_type", "authorization_code"); StringBuilder postData = new StringBuilder(); for (Map.Entry<String, Object> param : params.entrySet()) { if (postData.length() != 0) postData.append('&'); postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } byte[] postDataBytes = postData.toString().getBytes("UTF-8"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Host", "www.googleapis.com"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(true); conn.getOutputStream().write(postDataBytes); int responseCode = conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); // Build the string StringBuilder sb = new StringBuilder(); for (int c; (c = in.read()) >= 0; ) { sb.append((char) c); } // Convert JSON to a Java Object GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); AuthToken token = gson.fromJson(sb.toString(), AuthToken.class); // Disconnect conn.disconnect(); return token; } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(AuthToken token) { // Authorize Firebase with Games API firebaseAuthWithGamesApi(token.id_token); } }.execute(); } 调用了哪种方法?

1 个答案:

答案 0 :(得分:2)

首先,在baidu中,只有当百度控制台/您的API发送的消息属于透明类型时,才会将有效负载传送到应用程序的接收方。 您可以覆盖onMessage()以获取有效负载。