GCM推送通知到iOS与content_available(不工作从非激活状态调用)

我正在开发基于Java REST的Web服务,在这个服务中,我试图通过Google Cloud Messaging将消息从Java API发送到iOS设备。 为了学习的目的,我已经使用iOS的谷歌示例代码,我可以发送消息,当应用程序在前台,但它不工作时,应用程序在后台。 我已经尝试了几个变体的“content_available”标志,负责从后台调用应用程序。 当应用程序在前台时,它工作的很好。 我正在尝试显示应用程序在后台的通知。

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(); 

这里是我的iOS应用程序委托代码接收通知,基本上是谷歌示例代码添加代码显示通知

 // [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]]; } 

我已经尝试发送通知中的数据作为JSONstring与“content_available”,“content-available”和值变化的各种变化为'1'trueTRUE 。 这似乎不反映我的变化。 我曾经尝试发送“声音”作为“默认”,正如我在某些应该影响的问题中发现的那样。 我也已经实现了这个android也是一样的魅力。 基本上,据我所知,我已经通过gcm文档和APNS文档获得了它应该调用由“content-available”决定的第二个方法,但它不适用于我。

这是一个链接到谷歌文档与content_available。

https://developers.google.com/cloud-messaging/server-ref#downstream

https://developers.google.com/cloud-messaging/server#payload

要查看“content_available”的部分内容,请通过页面search以获取有关信息。

我通过彻底查阅文档解决了这个问题,我的新工作的java代码看起来像这样。

 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(); }