Gcm Support for iOS Application When Application in Background or Killed

GCM support for ios application when application in background or killed

I had same problem and I posted detailed question later. Luckily, I figured out the problem and it is working for me now. I was not sending data in proper format that got working after I sent data in proper format. My JSON format looks like this.

{
"notification":{
"badge":"12",
"alert":"default",
"sound":"default",
"title":"default"
},
"content_available":true,
"to":"YOUR_KEY_HERE"
}

Since there isn't details available on your sever implementation. you can refer my question for more information.

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

Making GCM work for iOS device in the background

For every poor soul wondering in quest for an answer to GCM background mystery. I solved it and the problem was in the format. I'm posting the right format as well as Java code needed to send Http request to GCM with some message.
So the Http request should have two field in the header, namely:

Authorization:key="here goes your GCM api key"
Content-Type:application/json for JSON data type

then the message body should be a json dictionary with keys "to" and "notification". For example:

{
"to": "gcm_token_of_the_device",
"notification": {
"sound": "default",
"badge": "2",
"title": "default",
"body": "Test Push!"
}
}

Here is the simple java program (using only java libraries) that sends push to a specified device, using GCM:

public class SendMessage {

//config
static String apiKey = ""; // Put here your API key
static String GCM_Token = ""; // put the GCM Token you want to send to here
static String notification = "{\"sound\":\"default\",\"badge\":\"2\",\"title\":\"default\",\"body\":\"Test Push!\"}"; // put the message you want to send here
static String messageToSend = "{\"to\":\"" + GCM_Token + "\",\"notification\":" + notification + "}"; // Construct the message.

public static void main(String[] args) throws IOException {
try {

// URL
URL url = new URL("https://android.googleapis.com/gcm/send");

System.out.println(messageToSend);
// Open connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// Specify POST method
conn.setRequestMethod("POST");

//Set the headers
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setDoOutput(true);

//Get connection output stream
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

byte[] data = messageToSend.getBytes("UTF-8");
wr.write(data);

//Send the request and close
wr.flush();
wr.close();

//Get the response
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

//Print result
System.out.println(response.toString()); //this is a good place to check for errors using the codes in http://androidcommunitydocs.com/reference/com/google/android/gcm/server/Constants.html

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Google Cloud Messaging: don't receive alerts when iOS App is in background

Based on the GCM documentation, you can set the content_available to true.

(On iOS, use this field to represent content-available in the APNS payload. When a notification or message is sent and this is set to true, an inactive client app is awoken. On Android, data messages wake the app by default. On Chrome, currently not supported.)

The content_available is correspond to Apple's content-available, which you can find in this Apple Push Notification Service documentation.

Also, you should use Notification playload, for sending message to your iOS application, so that it can show a banner when your app is in background.

Here's a sample HTTP request:

https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=API_KEY
{
"to" : "REGISTRATION_TOKEN",
"notification" : {
"sound" : "default",
"badge" : "1",
"title" : "default",
"body" : "Test",
},
"content_available" : true,
}

The Java library is just a sample, you can add other fields to it. For example, in the Message.java class, you can add two private variables, one is private final Boolean contentAvailable, another one is private final Map<String, String> notification.

You can try a HTTP request in your terminal by doing curl -i -H "Content-Type:application/json" -H "Authorization:key=API_KEY" -X POST -d '{"to":"REGISTRATION_TOKEN", "notificaiton":{"sound":"default", "badge":"1", "title": "default", "body":"test",},"content_available":true}' https://android.googleapis.com/gcm/send, or try it in Postman.

Edited:

If your application was terminated, and you want to the push notifications to be shown in your device, you can set a high priority in your HTTP request body (beware that setting your messages to high priority contributes more to battery drain compared to normal priority messages).

Sample HTTP request:

{
"to" : "REGISTRATION_TOKEN",
"notification" : {
"sound" : "default",
"badge" : "1",
"title" : "default",
"body" : "Test",
},
"content_available" : true,
"priority" : "normal",
}

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

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

How to get push notification when app is not running/app is terminated

This is your push notification JSON?

aps: { "content-available" = 1; }

If yes, then you are sending a silent push. Silent push means that the user get's no visual notification, just the app delegate callback of your app is called. Remove the content-available tag and pass a message text instead.

If the App is in the foreground iOS does not show a push notification, but just calls the delegate. Then you can show an Alert View or something else you prefer.

Or you can show this: https://github.com/avielg/AGPushNote

Regarding the state "terminated by user", here the Apple doc (for silent push):

Apple documentation

Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. In addition, if you enabled the remote notifications
background mode, the system launches your app (or wakes it from the
suspended state) and puts it in the background state when a remote
notification arrives. However, the system does not automatically
launch your app if the user has force-quit it. In that situation, the
user must relaunch your app or restart the device before the system
attempts to launch your app automatically again.



Related Topics



Leave a reply



Submit