Payload 는 다음과 같은 형태로 보낸다.


Json Data


{

"aps":{

"alert":{

"title":"title",

"body":"Hi"

},

"badge":3,

"sound":"default"

}

}





Frame 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class Frame {
    private final int COMMAND_NOTIFICATION = 2;
 
    private final short ITEM_ID_DEVICE_TOKEN = 1;
    private final short ITEM_ID_PAYLOAD = 2;
    private final short ITEM_ID_NOTIFICATION_ID = 3;
    private final short ITEM_ID_EXPIRATION_DATE = 4;
    private final short ITEM_ID_PRIORITY = 5;
 
    private final int MAX_PAYLOAD_BYTES = 2048/// 2048 크기 이상 pay 로드에 싣을수 없음.
 
    private String device_token;
    private int frame_length;
 
    private byte[] frame_data;
 
    public Frame(String device_token) {
        this.device_token = device_token;
    }
 
    public byte[] getFrame_data() {
 
        return frame_data;
    }
 
    public int getFrame_length() {
 
        return frame_length;
    }
 
    public int getCommandNotification() {
        return COMMAND_NOTIFICATION;
    }
 
    public void pack() {
        
 
        // payload Data를 담는다.

        String payload = get_jsonData();

        try {
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            
            baos.write(ITEM_ID_DEVICE_TOKEN);
            baos.write(intTo2ByteArray(32));
            baos.write(DatatypeConverter.parseHexBinary(device_token));
            
        
            
 
            baos.write(ITEM_ID_PAYLOAD);
            baos.write(intTo2ByteArray(payload.getBytes().length));
            baos.write(payload.getBytes("UTF-8"));
    
            
            baos.write(ITEM_ID_NOTIFICATION_ID);
            baos.write(intTo2ByteArray(4));
            baos.write(intTo4ByteArray(5));
 
            
            baos.write(ITEM_ID_EXPIRATION_DATE);
            baos.write(intTo2ByteArray(4));
            baos.write(intTo4ByteArray(5));
            
            baos.write(ITEM_ID_PRIORITY);
            baos.write(intTo2ByteArray(1));
            baos.write((byte)10);
            
            
            
            frame_length = baos.size();
            frame_data = new byte[baos.size()];
            frame_data = baos.toByteArray();
 
baos.close();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    // json 데이터를 가져오는 과정
    public String get_jsonData() {
 
        JSONParser parser = new JSONParser();
        JSONObject jObj = new JSONObject();
        Object obj = null;
 
        try {
            String path =  Main.class.getResource("").getPath();
            obj = parser.parse(new FileReader(path));
            jObj = (JSONObject) obj;
 
            System.out.println("payLoad:" + jObj.toJSONString());
 
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return jObj.toString();
    }
 
    private static final byte[] intTo4ByteArray(int value) {
        return ByteBuffer.allocate(4).putInt(value).array();
    }
 
    private static final byte[] intTo2ByteArray(int value) {
        int s1 = (value & 0xFF00>> 8;
        int s2 = value & 0xFF;
        return new byte[] { (byte) s1, (byte) s2 };
    }
 
}
cs



-> 전체 Frame Data에 5개의 ITEM 추가 하였다.

 






Binary Provider API


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
 
import javax.net.ssl.SSLSocket;
 
 
public class Binary_Provider_API {
 
    private SSLSocket sSock;
    private String device_token;
 
    public Binary_Provider_API(SSLSocket sSock, String device_token) {
        this.sSock = sSock;
        this.device_token = device_token;
 
    }
 
    
    public void send() {
 
        try {
            Frame frame = new Frame(device_token);
            frame.pack();
            OutputStream outputStream = sSock.getOutputStream();
            outputStream.write(frame.getCommandNotification());
            System.out.println("api_send_command:" + frame.getCommandNotification());
 
            outputStream.write(intTo4ByteArray(frame.getFrame_length()));
            System.out.println("api_send_length:" + frame.getFrame_length());
 
            outputStream.write(frame.getFrame_data());
            System.out.println("Item Packet");
            System.out.println(Arrays.toString(frame.getFrame_data()));
 
            outputStream.flush();
outputStream.close();
 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
    private static final byte[] intTo4ByteArray(int value) {
        return ByteBuffer.allocate(4).putInt(value).array();
    }
cs



frame data를 가지고 온 이후, Binary Provider API Format에 맞게 보내면 끝.




command / frame_length / frameData





+ Recent posts