Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

objective c - GCM push notification to iOS with content_available (not working to invoke from inactive state)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...