APNs feedback handler

Important: On March 31 2021 Apple has discontinued the APNs legacy binary protocol. Therefore, the com.APNS class is now desupported. The documentation pages related to the APNS binary protocol are still provided for information. Contact your support center, if you need to implement APNS with the new HTTP/2-based provider API.

The com.APNS class can be used to implement a server application to query the APNs feedback service.

Implement an APNs feedback handler to get a list of unregistered device tokens in order to stop sending push notification messages to these apps.
Note: An SSL/TLS certificate needs to be defined in FGLPROFILE, as described in APNs SSL/TLS certificate.
IMPORT com
IMPORT security
IMPORT util

MAIN
  DEFINE req com.TCPRequest
  DEFINE resp com.TCPResponse
  DEFINE feedback DYNAMIC ARRAY OF RECORD
                      timestamp INTEGER,
                      deviceToken  STRING
                  END RECORD
  DEFINE timestamp DATETIME YEAR TO SECOND
  DEFINE i INTEGER
  DEFINE data BYTE

  LOCATE data IN MEMORY

  TRY
    LET req = com.TCPRequest.create( "tcps://feedback.push.apple.com:2196" )
    CALL req.setKeepConnection(true)
    CALL req.setTimeout(2)
    CALL req.doRequest()
    LET resp = req.getResponse()
    CALL resp.getDataResponse(data)
    DISPLAY "Feedback service has responded"
    CALL com.APNS.DecodeFeedback(data,feedback)
    FOR i=1 TO feedback.getLength()
      LET timestamp = util.Datetime.fromSecondsSinceEpoch(feedback[i].timestamp)
      DISPLAY "Device Token :",feedback[i].deviceToken, " Timestamp :", timestamp
    END FOR
  CATCH
    CASE STATUS
      WHEN -15553 DISPLAY "Timeout: No feedback message"
      WHEN -15566 DISPLAY "Operation failed :", SQLCA.SQLERRM
      WHEN -15564 DISPLAY "Server has shutdown"
      OTHERWISE   DISPLAY "ERROR :",STATUS
    END CASE
  END TRY

END MAIN