我正在开发基于Java REST的Web服务,尝试通过Google Cloud Messaging将消息从Java API发送到iOS设备。出于学习目的,我使用了Google iOS示例代码,并且当应用程序在前台时,我能够成功发送消息,但是当应用程序在后台时无法正常工作。我尝试了多种“content_available”标志的变化,这个标志负责在后台调用应用程序。当应用程序在前台时,它能够很好地工作。我正在尝试在应用程序在后台时显示通知。
我尝试将数据作为JSON字符串发送到通知中,使用各种变化的"content_available"、"content-available"和值的变化为'1'、true、TRUE。似乎没有反映我的更改。我尝试将'sound'发送为'default',因为我在一些问题中发现它应该会受到影响。我已经在Android上实现了这个功能,而且它的效果非常好。基本上,根据我通过GCM文档和APNS文档获得的知识,它应该调用一个由"content-available"决定的第二个方法,但对我来说却不起作用。
这是一个带有content_available的Google文档链接。
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]];
}
我尝试将数据作为JSON字符串发送到通知中,使用各种变化的"content_available"、"content-available"和值的变化为'1'、true、TRUE。似乎没有反映我的更改。我尝试将'sound'发送为'default',因为我在一些问题中发现它应该会受到影响。我已经在Android上实现了这个功能,而且它的效果非常好。基本上,根据我通过GCM文档和APNS文档获得的知识,它应该调用一个由"content-available"决定的第二个方法,但对我来说却不起作用。
这是一个带有content_available的Google文档链接。
https://developers.google.com/cloud-messaging/server-ref#downstream
https://developers.google.com/cloud-messaging/server#payload
要查看“content_available”的部分,请搜索页面以获取有关该内容的信息。