GCM push notification for iOS with content_available (does not work for calling from an inactive state)

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

 // [START ack_message_reception] - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@" foregraound one Notification received: %@", userInfo); // This works only if the app started the GCM service [[GCMService sharedInstance] appDidReceiveMessage:userInfo]; // Handle the received message // [START_EXCLUDE] [[NSNotificationCenter defaultCenter] postNotificationName:_messageKey object:nil userInfo:userInfo]; // [END_EXCLUDE] UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@"Hello world"]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler { NSLog(@" backgroun one Notification received: %@", userInfo); // This works only if the app started the GCM service [[GCMService sharedInstance] appDidReceiveMessage:userInfo]; // Handle the received message // Invoke the completion handler passing the appropriate UIBackgroundFetchResult value // [START_EXCLUDE] [[NSNotificationCenter defaultCenter] postNotificationName:_messageKey object:nil userInfo:userInfo]; handler(UIBackgroundFetchResultNoData); // [END_EXCLUDE] UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@"Hello world"]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; } 

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.

+3
source share
1 answer

I solved the problem by accessing the documents completely, my new working Java code is as follows.

 JSONObject subobj = new JSONObject(); subobj.put("sound", "default"); subobj.put("badge", "12"); subobj.put("title", "default"); JSONObject obj = new JSONObject(); obj.put("to", regisID); obj.put("notification", subobj); obj.put("content_available", new Boolean(true)); post.setHeader("Authorization", "key=MyKey"); post.setHeader("Content-Type", "application/json"); try { HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 
+5
source

All Articles