制作GCM工作的iOS设备在后台 [英] Making GCM work for iOS device in the background

查看:282
本文介绍了制作GCM工作的iOS设备在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GCM iOS和Android客户端。它似乎好工作与IOS应用程序时,在前台,然而,当应用程序在后台,通知中心没有收到消息,didReceiveRemoteNotification与completionHandler不会被调用。

I'm trying to use GCM for IOS and Android clients. It seems to work fine with IOS when app is in the foreground, however, when the app is in the background, the notification center doesn't receive the message and didReceiveRemoteNotification with completionHandler doesn't get called.

我发现的问题从GCM到APNS一个错误格式的消息。也就是说,这是它的外观:
[消息:新的消息,collapse_key的:do_not_collapse,来自:**************]
同时,IOS的推送通知应在通知APS键,对吗?除了内容可设置为1。例如:

I identified a problem as a wrongly formatted message from GCM to APNS. Namely, that's how it looks: [message: New message, collapse_key: do_not_collapse, from: **************] While, the IOS push notifications should have "aps" key in the notification, am I right ? As well as content-available set to 1. For example:

{
    APS:{
        内容提供:1
    },
    数据ID:345
}

{ "aps" : { "content-available" : 1 }, "data-id" : 345 }

顺便说一下,在前台应用程序接收消息无论如何,问题是仅与背景。我应该如何处理的问题有什么建议,使GCM工作为iOS和Android?

By the way, in the foreground app receives the message anyway, the problem is only with the background. Any advice on how should I approach a problem, to make GCM work for both ios and android?

更新
这就是我在网上找到:

UPDATE: That is what I found on the net:

对于实际的通讯,只要应用程序在iOS设备上的背景下,GCM使用APNS发送消息,该应用程序同样表现为使用苹果的通知系统。但是,当应用程序被激活,GCM与应用程序直接通信

Regarding actual communication, as long as the application is in the background on an iOS device, GCM uses APNS to send messages, the application behaving similarly as using Apple’s notification system. But when the app is active, GCM communicates directly with the app

所以我在前台模式下收到的消息:

So the message I received in the foreground mode:

[消息:新的消息,collapse_key的:do_not_collapse,来自:**************]

[message: New message, collapse_key: do_not_collapse, from: **************]

从GCM直接消息(APNS没有参与这件事的话)。所以,问题是:APNS格式化什么GCM发送给它坚持到iOS的通知格式?如果是的话我怎么知道,其实APNS做一些事情,以及是否将我不同格式的通知?有没有办法从APNS查看输入数据的日志?

Was the direct message from GCM(APNS did not participate in this affair at all). So the question is: does APNS reformat what GCM sends to it to adhere to ios notifications format? If so how do I know that APNS actually does something and whether it sends me a notification in different format ? Is there any way to view logs of incoming data from APNS ?

更新:
好吧,我设法改变消息的结构现在在前台模式我收到以下消息:

UPDATE: Okay, I managed to change the structure of the message and now in the foreground mode I receive the following message:

收到通知:APS:{警告:简单的信息,内容提供:1},collapse_key的:do_not_collapse,来自:************** ]

Notification received: ["aps": {"alert":"Simple message","content-available":1}, collapse_key: do_not_collapse, from: **************]

现在,它似乎很好地格式化,但是仍然没有反应,当应用程序在后台。 didReceiveRemoteNotifification completionHandler不会被调用!我应该怎么找和一个问题,可以吗?方形支架可用于推送通知的问题吗?为了更加precise,iOS不发布任何警报/徽章/从来电通知横幅。

Now it seems to be well formatted, but there is still no reaction when the app is in the background. didReceiveRemoteNotifification completionHandler doesn't get called! What should I look for and where can a problem be ? Can the square bracket be a problem for push notification ? To be even more precise, ios doesn't post any alerts/badges/banners from that incoming notification.

推荐答案

对于每一个可怜的灵魂在寻求疑惑的答案,GCM背景神秘。我解决了它,问题是格式。我张贴正确的格式,以及发送HTTP请求到GCM一些消息所需的Java code。
所以HTTP请求应该有两个字段在头,即:

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

然后在邮件正文应该是一个JSON字典的钥匙到和通知。例如:

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!"
  }
}

下面是发送推到指定的设备,使用GCM的简单的Java程序(仅使用Java库):

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

这篇关于制作GCM工作的iOS设备在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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