I am working on a Java REST-based web service in which I am trying to send messages from the Java API to an iOS device through Google Cloud Messaging. For training purposes, I used a sample Google code for iOS, and I can send messages when the application is in the foreground, but it does not work when the application is in the background. I tried several options for the flag "content_available", which is responsible for calling the application from the background. It works well when the application is in the foreground. I am trying to show notifications when the application is in the background.
HttpClient client = new DefaultHttpClient(); HttpPost post = null; try { post = new HttpPost("https://android.googleapis.com/gcm/send"); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } String regisID="My_iOS_Registration_Id-GVnH1gEsJ"; List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); List<NameValuePair> notificationData = new ArrayList<NameValuePair>(1); notificationData.add(new BasicNameValuePair("title", "title")); JSONObject obj=new JSONObject(); obj.put("title", "title"); obj.put("alert", "title"); obj.put("sound", "default"); obj.put("badge", "1"); nameValuePairs.add(new BasicNameValuePair("to", regisID)); nameValuePairs.add(new BasicNameValuePair("notification", obj.toString())); nameValuePairs.add(new BasicNameValuePair("content_available", "true")); post.setHeader("Authorization", "key=MyKey"); try { HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpResponse response = null; try { response = client.execute(post); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity entity1 = response.getEntity(); try { System.out.println("Hi response is : " + EntityUtils.toString(entity1)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response.getStatusLine().toString();
Here is my delegate notification app code for iOS, which is basically an example Google code with added code to show notifications
I tried to send the data in the notification as a JSON string with various options for "content_available", "content-available" and variations of the values '1' , true , true . This does not seem to reflect my changes. I tried to send the βsoundβ as βdefaultβ, as I found in some of the issues that it should address. I also implemented this for android, and it works like a charm. Basically, according to my knowledge, I got the gcm documentation and APNS documentation, it should call the second method defined by "accessible content", but it does not work for me.
Here is a link to google documentation with content_available.
https://developers.google.com/cloud-messaging/server-ref#downstream
https://developers.google.com/cloud-messaging/server#payload
To view the "content_available" part, search the page for more information.