Put sound in app notifications

Asked

Viewed 99 times

1

I’m having trouble with my application, I’m trying to send notifications to mobile devices, notifications arrive but they do not present sound and I need them to make the sound to draw attention. I took a look at some codes, but I didn’t understand/couldn’t implement.

public static void sendPush(final String title, final String message, final String id, Object... args) {
    if (!Utils.isEmpty(id)) {
        ConfigUtils cfg = ConfigUtils.get();
        String serverKey = cfg.get("gcm.server.key", "CHAVE-API");

        if (serverKey == null) {
            LogUtils.info("API Key não encontrada, registrar app em https://developers.google.com/mobile/add?platform=android&cntapi=gcm");
            return;
        }

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    HttpParams myParams = new BasicHttpParams();

                    HttpConnectionParams.setSoTimeout(myParams, 10000);
                    HttpConnectionParams.setConnectionTimeout(myParams, 10000); // Timeout

                    String url = GCM_URL;
                    DefaultHttpClient client = new DefaultHttpClient(myParams);
                    HttpPost post = new HttpPost(url);
                    BasicHttpContext localContext = new BasicHttpContext();
                    JSONObject mainData = new JSONObject();
                    try {
                        JSONObject data = new JSONObject();
                        data.putOpt("text", message);
                        data.putOpt("title", title);

                        JSONArray regIds = new JSONArray();

                        regIds.put(id);
                        mainData.put("registration_ids", regIds);
                        mainData.put("notification", data);
                        mainData.put("content_available", true);
                        mainData.put("priority", "high");

                        if (args != null && args.length > 0) {
                            Map toMap = Utils.toMap(args);

                            toMap.put("coldstart", true);
                            toMap.put("foreground", true);

                            mainData.put("data", toMap);
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    StringEntity se = new StringEntity(mainData.toString(), "UTF-8");
                    post.setEntity(se);
                    post.addHeader("Authorization", "key=" + serverKey);
                    post.addHeader("Content-Type", "application/json");
                    post.addHeader("Accept", "application/json");
                    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
                    HttpResponse response = client.execute(post, localContext);

                    BufferedReader rd = new BufferedReader(
                            new InputStreamReader(response.getEntity().getContent()));
                    StringBuilder result = new StringBuilder();
                    String line;
                    while ((line = rd.readLine()) != null) {
                        result.append(line);
                    }
                    System.out.println("Result: " + result + ", Code: " + response.getStatusLine().getStatusCode());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        thread.start();
    }
}
  • A good tip is to use Onesinal, extremely simple and very complete, and you will have many clicks statistics and other various features

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.