Chapter 6/7- can't receive a notification

What’s wrong:
I can’t send push notifications by running sendPushes.php script from Chapter 6 (the script itself is located in Chapter 7 folder).

At first I thought that this problem was caused by Docker but I think that’s not the case since I’m able to send notifications to a device using sendEncrypted.php (by additionally providing device’s token) script from Chapter 9.

What I’ve already tried to solve this problem:

  • Since I was able to send a single notification using sendEncrypted.php then it’s safe ot assume that AUTH_KEY_PATH, AUTH_KEY_ID, TEAM_ID, BUNDLE_ID are correct.
  • I’ve added the missing line in chapter 6

I’m pretty sure, that the problem might be caused by the fact that none of the tokes are actually saved to the database ( print_r($stmt->fetchAll(PDO::FETCH_COLUMN, 0)); always prints an empty array).

Any ideas what could be causing this problem?

If your tokens are blank you’ll definitely get no pushes. In chapter 6 there’s a section on (I think) page 61 for “Testing your API”. Are you able to successfully perform the steps shown there? When you press send, and look at the Response body, do you see a token displayed?

1 Like

I managed to fix it! Turns out, all I had to do was to remove all images from Docker after I added the missing line ( static let entity = "tokens")

For anyone having similar problem

  • kill all running containers with docker kill $(docker ps -q)
  • delete all stopped containers with docker rm $(docker ps -a -q)
  • delete all images with docker rmi $(docker images -q)
  • run docker run --name postgres -e POSTGRES_DB=apns -e POSTGRES_USER=apns -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres again

@clapslock Thank you for sharing the solution - much appreciated! :]

I am having these same issues. I do get the response body and token. When you say run sendPushes.php what do you mean? Upload to local server and access sendPushes.php?

In the download for the chapter there is the sendPushes.php script you copied to your Mac and edited. From the Terminal you just run “php sendPushes.php”

Thanks nothing happens when I run this after cd into the directory it is located. I think the error may be somewhere else. When I install Vapor should I be able to access localhost at my ip address :8080/api/token? When I do this in rested the response body returns the unique string id and the token, but when I access it from local host i just get Not Found. Also this error is showing up. TIC TCP Conn Failed [1:0x283cb7000]: 1:53 Err(53)

I’m not sure what you are saying. If you use something like rested you’re putting http://localhost:8080/api/token. That’s the same thing you’d put for any other type of method.

From where exactly are you getting that TCP error?

What I am saying is if I enter http://localhost:8080/api/token in a browser I get not found, but if I put it into rested it comes back with the token string. Is all I need is the json response from rested? The TCP error happens when I run the PushNotification.swift file and touch anything once it launches. This TCP code displays in the output console in xcode.

@jessemfulton It sounds like you have something configured strangely on your web browser. Maybe you’re going through some type of proxies? The fact that rested is getting the JSON back means that your Docker, SQL, and Vapor instances are all running correctly.

I’m not sure what you mean though by “run the PushNotification.swift” file. You’d run your iOS app and that will register with the Vapor instance. You’ve done that as you’re seeing JSON come back. If you run sendPushes.php then it will connect to the vapor instance, pull the tokens out, and then send the appropriate notification to those devices.

Your right all of these instances are running correctly. The problem is with the sendPushes.php. When I run this nothing happens and I am not sure why. I need to look into this more to figure that out. The TCP error shows once I run the iOS app and touch anything. I just wasn’t sure why it was doing this. Thought someone might know.

@jessemfulton Can you post the full error you get? Usually, you should get a URLSession error, not just the genera TCP error in the console.

Here is the full error.

e2236babacccf2d504a1162f5f89d373daec71f69be1abbb72a5b67c058bcc8e

2019-04-02 16:20:44.055314-0700 PushNotifications[33973:9703650] TIC TCP Conn Failed [1:0x283cb7000]: 1:53 Err(53)

2019-04-02 16:20:44.056406-0700 PushNotifications[33973:9703650] Task <7223469D-CA14-4D88-B535-8A8AE41D5212>.<1> HTTP load failed (error code: 53 [1:53])

2019-04-02 16:20:44.056651-0700 PushNotifications[33973:9703648] Task <7223469D-CA14-4D88-B535-8A8AE41D5212>.<1> finished with error - code: 53

Sorry I don’t mean to hijack this post topic, but I have gone through every line in the push notifications pdf multiple times, have deleted and started over multiple times and get everything working with one exception. When it comes to using terminal and sending sendPushes.php I get no notification. I cant understand why. Let me detail what I have done so maybe someone can see something I am doing wrong.

  1. I am using docker and created database user and password.
  2. I set up Azure Data Studio and connected to the Docker database so I could create the tokens table with the id, token text and debug columns.
  3. I installed Homebrew so i could then install vapor.
  4. I navigated to the root folder of the ch.6 starter project and ran the command to create the vapor xcode project.
  5. I created the model, controller, and routes in the vapor project.
  6. I downloaded rested and set up my test endpoint and get back in the response body the id, token, and debug parameters.
  7. I created sendPushes.php and put in the path to my AuthKey_keyid.p8 which I put in the same starter folder for ch.6 along with the sendPushes.php. I have also tried putting this into the ch.7 folder. Both options dont work. I entered my Auth_Key_ID, TEAM_ID, and BUNDLE_ID, in which all are correct.
  8. Now in the ch.7 PushNotifications project I set up all of the steps, modified the AppDelegate to update the server, edited the info.plist to create a key for the App Transport Security Settings.
  9. The final step is to run the sendPushes.php script which I do in terminal by running php sendPushes.php. When I run this it seems like it is doing something, but I get no notification.

What am I missing? Please respond. I also did add the line
static let entity = “tokens”
which didn’t do anything.

You shouldn’t be doing step 2. When you run the Vapor project it will create the tables appropriately. Please use Azure Data Studio to delete all tables that exist in the database and then rerun the vapor server so it creates them for you.

Did you manually insert the token or did you let the iOS app create it for you?

For step 7, did you enter the full path to the .p8 file? The pathname should start with a / character for it to work properly.

You’ve got sendPushes.php pointing at Apple’s development server, not the production server, right?

Your error means that it’s not properly connecting to your vapor server. It really seems like you’ve got something strange going on with your networking at your location, or the Vapor server isn’t configured with the proper IP address.

@gargoyle Oh ok. The way the pdf was written is confusing to me. It looks as if I needed to set up my own sql server that connects to docker db I created and create the table. Nowhere does it say that the vapor project will create it for you.

As far as the token goes I let the iOS app create it.

For step 7 I put the .p8 file in the same folder as the project, but did not put a slash in front of the file name. And yes sendPushes.php is pointing to Apples dev server.

@gargoyle just to confirm I just need to use Docker and vapor for this project along with xcode of course. Docker acts as my virtual server and vapor as my local server.

Docker is basically a virtual server, and it is what is running PostgreSQL for you. Most people don’t have a SQL server installed on their mac and so this is the easiest way to put one there.

Vapor is server side Swift (see our book dedicated to Vapor) and that is what runs your web server for you. It runs on your local machine directly. When the vapor server tries to open a connection to the SQL server, your machine redirects it to the PostgreSQL instance running inside of Docker.

I’ll try to explain all of this better in the next release of the book.

@gargoyle Ok I did all that you asked and everything is working. I am not longer getting any errors, but am still not receiving any notifications when I use sendPushes.php. Not sure what I am missing.

I did notice when I run the vapor server I dont get the second line in this statement. Could this be what is causing the problem?

[ INFO ] Migrating ‘psql’ database (FluentProvider.swift:28)

[ INFO ] Preparing migration ‘Token’ (MigrationContainer.swift:50)

[ INFO ] Migrations complete (FluentProvider.swift:32)

Running default command : /Users/scott/Library/Developer/Xcode/

DerivedData/dts-fdkxzqveujzmdycmruvnzrydjvwn/Build/Products/Debug/Run

serve

Server starting on http://192.168.1.1:8080