GCM 使用 content_available 向 iOS 推送通知(无法从非活动状态调用) [英] GCM push notification to iOS with content_available (not working to invoke from inactive state)

查看:26
本文介绍了GCM 使用 content_available 向 iOS 推送通知(无法从非活动状态调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于 Java REST 的 Web 服务,我正在尝试通过 Google Cloud Messaging 将消息从 Java API 发送到 iOS 设备.出于学习目的,我使用了适用于 iOS 的谷歌示例代码,当应用程序在前台时我可以发送消息,但当应用程序在后台时它无法工作.我已经尝试了负责从后台调用应用程序的content_available"标志的几种变体.当应用程序处于前台时,它运行良好.我正在尝试在应用处于后台时显示通知.

I am working on Java REST based web service in which I am trying to send messages from a Java API to an iOS device through Google Cloud Messaging. For learning purposes I have used google sample code for iOS and I am able to send messages when the app is in the foreground but it is not working when the app is in the background. I have tried several variations of the "content_available" flag that is responsible for invoking the app from the background. It is working nicely when the app is in foreground. I am trying to show notifications when the app 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();

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

Here is my iOS app delegate code for receiving notifications that is basically the google sample code with added code for showing 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]];
}

我尝试将通知中的数据作为 JSON 字符串发送,其中包含content_available"、content-available"的各种变体,并且值变体为 '1'true,<代码>真.它似乎没有反映我的变化.我尝试将声音"作为默认"发送,因为我在一些应该影响的问题中发现.我也为 android 实现了这个,它就像一个魅力.基本上,根据我通过 gcm 文档和 APNS 文档获得的知识,它应该调用由内容可用"决定的第二种方法,但它对我不起作用.

I have tried sending data in the notification as a JSON string with various variations of "content_available", "content-available" and value variations to be '1', true, TRUE. It seems to be not reflecting to my changes. I have tried sending 'sound' as 'default' as I found in some questions that it should affect. I have implemented this for android also and it is working like a charm. Basically, according to my knowledge I have gained through gcm documentation and APNS documentation it should invoke a second method decided by "content-available" but it's not working for me.

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

Here is a link to google documentation with content_available.

https://developers.google.com/cloud-messaging/server-参考#下游

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

要查看content_available"的一部分,请搜索​​页面以获取有关它的信息.

To see part of 'content_available' please search through page to get information about it.

推荐答案

我通过彻底参考文档解决了这个问题,我的新 Java 代码如下所示.

I solved the problem by referring to documents thoroughly my new working java code looks like this.

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

这篇关于GCM 使用 content_available 向 iOS 推送通知(无法从非活动状态调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆