ios - GCM 使用 content_available 向 iOS 推送通知(无法从非事件状态调用)

标签 ios objective-c iphone push-notification google-cloud-messaging

我正在开发基于 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 应用程序委托(delegate)代码,用于接收通知,它基本上是谷歌示例代码,添加了用于显示通知的代码

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

我尝试将通知中的数据作为 JSON 字符串发送,其中包含“content_available”、“content-available”的各种变体,值变体为 '1'true。它似乎没有反射(reflect)出我的变化。我曾尝试将“声音”发送为“默认”,因为我在一些问题中发现它应该会影响。我也为 android 实现了这个,它工作得很好。基本上,根据我通过 gcm 文档和 APNS 文档获得的知识,它应该调用由“内容可用”决定的第二种方法,但它对我不起作用。

这里是带有 content_available 的谷歌文档链接。

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

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

要查看“content_available”的一部分,请搜索​​页面以获取相关信息。

最佳答案

我通过彻底引用文档解决了这个问题,我的新工作 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();
}

关于ios - GCM 使用 content_available 向 iOS 推送通知(无法从非事件状态调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893152/

相关文章:

ios - MPMoviePlayerController 从第 30 秒开始播放视频(从视频中间开始)

php - 使用帐户获取/解析数据库数据

ios - 强制 UICollectionView 滚动直到特定单元格居中

iphone - 使用 CFURLRef 从服务器显示 PDF

objective-c - 在 iOS 中线程化 TCP Controller

iOS facebook 通过自定义 View 登录(不是通过外部浏览器)

iphone - iPhone:YAJL被AppStore接受使用吗?

ios - 隐藏 iOS 导航栏中的所有内容,包括图标

ios - 使用 iTunes 11 安装 IPA

ios - 使用 github 的 Github 命令行 vs. Xcode vs. Git 应用程序?