Apple Push Notification Test

I use node.js (server framework) and mongoose.js (mongo-based circuit modeling) as the backend for an iOS application, and I use Mocha (test framework) to make sure everything works.

What I really want to know and cannot find any documentation is how to test on the server if push notifications are sent properly. I am using the upgrade and at the moment I see that push notifications are sent correctly by manually checking my device, but itโ€™s hard for me to find an automated way to verify that they are working correctly.


This may be enough description to answer at a high level what needs to be done. But in case this is not real code:

The Mongoose model triggers a push notification when it is created:

#this code is called after this model is saved in mongodb eventModel.post 'save', (doc) -> #push the message sendMessageToDevice = (event, token) -> message = event_body: eventId: event._id lat: event.lngLat[1] lng: event.lngLat[0] agent.createMessage() .device(token) .alert('New Event! ' + event.description) .set(message) .send() #cycle through the users to push to #get all the unique device tokens in the database for APN users.getAllUniqueDeviceTokens (error, devices) -> if error then return util.handleError error console.log "Sending push notices to all devices (%d):", devices.length console.log devices for token in devices sendMessageToDevice doc, token #send some verification here that the code ran correctly??? 

Then in my Mocha test file I have:

 it 'should receive push notification from fort creation', (done) -> #some logic here to verify that push notifications were sent done() 
+5
source share
3 answers

In many situations, when writing tests, this is either impossible or simply too dangerous to verify that the action actually took place (i.e. a push notification was made). Imagine a unit test entry for the rm command, where you want rm -rf / succeed. Obviously, you cannot allow this action and make sure that your root partition is really empty!

However, you can (really, really do) make sure that all the commands, subroutines, or other actions necessary to complete the task run correctly, without actually allowing them to take place.

In your specific situation, you do not need to verify that your push notification has been delivered, because your application is not responsible for the delivery of notifications. However, you can verify that the push message is delivered to the push server correctly.

So, instead of checking for successful delivery, you will test

  • Is the outgoing request formatted correctly (e.g. JSON)
  • Will they contain the data that you expect from it (for example, a field in JSON is present and contains the expected data)
  • Is the authentication token required by the server enabled
  • Is the destination server correct (i.e. you are really sending data to xxx.apple.com and not to localhost).

Ideally, these test requests will not even reach the target server - this means that you rely on two factors that are not always completely stable:

  • network connection
  • target server availability and proper functionality

In the past, I dealt with this in order to manually issue the correct request first, grab the response, and then ridicule the entire message in unit test (using nock . This way I have full control over the whole message.

+3
source

As far as I know, there is no way to check if the APNS request has reached or not. Apple tends to have this โ€œall right, and if not, it should be your mistakeโ€ with us developers. If nothing has changed since the start of encoding, you make an APNS request by sending raw data (JSON payload, you probably know the whole format) through port 2195, and you will get absolutely no answer for that.

The only thing I can think about, if you have a physical iOS device (iPod, iPhone or iPad), you can "automate" the test by running a PUSH request with a hard-token installed that matches your device and a test application, and if you receive a notification , it will work.

Oh, and if that doesnโ€™t work, make sure that you have all the ports open if you are working behind a firewall. This is the first big stone I entered when I first looked into it;) (related: https://support.apple.com/en-us/HT203609 )

+2
source

I would like to use mocking framework like nock to intercept APN request. The urls seem to be in the code here .

+1
source

All Articles