GCM推送通知到iOS与content_available(不工作从非激活状态调用) [英] GCM push notification to iOS with content_available (not working to invoke from inactive state)

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

问题描述

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

  HttpClient client = new DefaultHttpClient(); 
HttpPost post = null;
尝试{
post = new HttpPost(https://android.googleapis.com/gcm/send);
} catch(URISyntaxException e){
// TODO自动生成的catch块
e.printStackTrace();
}
字符串regisID =My_iOS_Registration_Id-GVnH1gEsJ;
列表< NameValuePair> nameValuePairs = new ArrayList< NameValuePair>(1);
列表< 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);
尝试{
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
} catch(UnsupportedEncodingException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}
尝试{
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch(UnsupportedEncodingException e){
// TODO自动生成的catch块
e.printStackTrace();
}
HttpResponse response = null;
尝试{
response = client.execute(post);
} catch(HttpException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
HttpEntity entity1 = response.getEntity();
尝试{
System.out.println(Hi response is:+ EntityUtils.toString(entity1));
} catch(ParseException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
return response.getStatusLine()。toString();

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

  // [START ack_message_reception] 
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@foregraound one received notification:%@,userInfo);
//这只在应用程序启动GCM服务时有效
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
//处理收到的消息
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
// [END_EXCLUDE]


UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.repeatInterval = NSDayCalendarUnit;
[通知setAlertBody:@Hello world];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; (void)(void(^)(UIBackgroundFetchResult))

$ b - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:处理程序{
NSLog(@backgroun one Notification received:%@,userInfo);
//这只在应用程序启动GCM服务时有效
[[GCMService sharedInstance] appDidReceiveMessage:userInfo];
//处理收到的消息
//调用完成处理程序,传递相应的UIBackgroundFetchResult值
// [START_EXCLUDE]
[[NSNotificationCenter defaultCenter] postNotificationName:_messageKey
object:nil
userInfo:userInfo];
处理程序(UIBackgroundFetchResultNoData);
// [END_EXCLUDE]

UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.repeatInterval = NSDayCalendarUnit;
[通知setAlertBody:@Hello world];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];





$ b我已经尝试将通知中的数据作为JSON字符串发送, content_available,content-available和值变化为'1', true TRUE 。这似乎不反映我的变化。我曾尝试将声音发送为默认,正如我在某些应该影响的问题中发现的那样。我也为Android实现了这一点,它就像一个魅力。基本上,根据我的知识,我通过gcm文档和APNS文档获得了它应该调用第二种方法,由内容可用决定,但它不适合我。



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



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





要查看'content_available'的部分内容,请通过页面搜索以获取有关信息。

解决方案

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


$ b

  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);
尝试{
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
} catch(UnsupportedEncodingException e1){
// TODO自动生成的catch块
e1.printStackTrace();
}


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

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

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.

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 see part of 'content_available' please search through page to get information about it.

解决方案

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推送通知到iOS与content_available(不工作从非激活状态调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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