Push Notifications Tutorial: Getting Started

Please Can I get the Objective-C version of this tutorial or the previous versions of the article

Hi Jack
Nice tutorial! how can i push notifications from an online website of mine?

Absolutely awesome man, great great tutorial.

I used AWS SNS as my Push Notification Service, one thing to point out to everyone (probably many have figured out) is :
If you created your Apple developer app and APNS certificate in the development environment, you will want to set APNS_SANDBOX as your platform to push the notification to.

Did you try delete the app on the device and re-run the project?

take Amazon SNS as an example: Mobile push notifications - Amazon Simple Notification Service
you can download their sample code, use the AmazonSNSClientWrapper class in the codebase of your website code and then push notification to your ios app.

Hi, thanks for the great tutorial! I have a question though which seems to deal with quite a specific problem and which I am trouble finding any advice for. My question regards the background fetching of data once a silent notification is received. First of all, for background modes for the app, do we only need to enable push notifications or background fetch as well? When Iā€™ve tested so far, the app does get into the ā€œdidreceiveremotenotificationā€ method fine, however the processing inside that method just seem to stop, but that app doesnā€™t freeze, so I donā€™t know if the OS is impeding my app from doing background work. What I am trying to do is download messages from a server in the background, but to do that I am simply calling a function that authenticates with the server because the messages will download automatically when we are authenticated. All of this functionality happens in a different class and I am logging a lot of information so that I can see where the pause occurs. So I am calling the authenticate function from the ā€œdidreceiveremotenotificationā€ method, but the all the processing and stuff happens in a different class. At some point I send a notification for which the AppDelegate is listening and then itā€™s supposed to call completionhandler. For some reason I am not getting very far into the process before the processing stops. If I put a sleep command, I do get further sometimes and I even get to the completiong handler sometimes, but not all the time. I am just wondering if you could please help me understand the situation. Do we need to do all the work inside the ā€œdidreceiveremotenotificationā€ method? I doubt that. Can we call the completionhandler when a notification is received. I assume so, but I am missing something I think.

Any help would be much appreciated!

Regards,

Hermann Kuschke

I tried a lot with this problem but without any result I searched a lot and Iā€™m sure from everything about permission,port number and certificate but this error is appear to me at all time

Warning: stream_socket_client(): SSL: Connection reset by peer in /Users/alaa/Desktop/WenderCast-Starter/WenderCastPush/newspush.php on line 24

Warning: stream_socket_client(): Failed to enable crypto in /Users/alaa/Desktop/WenderCast-Starter/WenderCastPush/newspush.php on line 24

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/alaa/Desktop/WenderCast-Starter/WenderCastPush/newspush.php on line 24
Failed to connect: 0

How I can solve that, please :pensive:

1 Like

Hey Developers, It is always difficult and time consuming to test push notification and its redirection with web team. As an iOS Developer I wanted this to be under my control so that I can test whenever needed. To help other developers and freelancers I have created a simple web app in my free time to test Push Notifications (APNS) and Google Cloud Messaging (GCM). Check it out http://www.pushtry.com

1 Like

hi Jack,

Nice tutorail,

Please tell me it is possible send image using APNS?

if it is possible then how can send it using in ios.

Thanks!

I am getting this error as well. Did you resolve it?

Jack_Wuā€¦anybody. I just started this tutorial yesterday and I have done it over a ton of times and I get the same results. The last post of this forum was my inquiry from Alaaaā€™s entryā€¦same exact issue. Iā€™ve used apn tester and can send notifications via that tool but I can not get the php script to do it. Please help.

I struggled with this. I was getting the following:

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failedā€¦

At some point in my searching the Internet I ran across the following suggestion ā€“ add this line of code:

stream_context_set_option($ctx, ā€˜sslā€™, ā€˜verify_peerā€™, false);

This worked, but it deletes the purpose of SSL because it disables the script from verifying that it is talking with APNS. I didnā€™t want that as my long term solution, So I kept searching and ran across this post:

[ios - Push Notification in PHP using PEM file - Stack Overflow]

Sandy D.'s solution worked great!

Hey Jack,

Thanks for the wonderful tutorial. It was very helpful indeed.
However, I have a different kind of problem when handling push notification. In my project, we have a number of view controllers which we need to display depending on the type of data received in the notification. For this, we have created a class which parses the data and has a switch case to which initializes the controller from the push notification and pushes it on the stack. But the switch case causes the app to be very brittle. A small change here or there and the push stop working and has lately become highly unmaintainable. I was wondering if you had a cleaner way to handle this kind of scenario.

1 Like

I advise you to use this great tool that allows to send Push Notifications : NWPusher

You can but keep in mind that the payload size is limited to 2 kilobytes. I suggest you send the link to the image in the push notificationā€™s payload then you download it when you should respond to the push notification.

Would it be possible to get the userā€™s location in a silent alert and send that location to the server?
Iā€™m sending a notification with ā€œcontent-availableā€: 1 and starting a location manager but the locationManager:(CLLocationManager *)manager didUpdateLocations: does not get called in the back ground. Iā€™m trying to avoid tracking location constantly by requesting it only when itā€™s needed via a push notification.

Please use the following command to build the .pem file .

openssl pkcs12 -in M8CardPushProd.p12 -out ckp.pem -nodes -clcerts

Also do not for get to add the php code line below to your php push test file.
stream_context_set_option($ctx, ā€˜sslā€™, ā€˜verify_peerā€™, false);

Any chance youā€™ll be updating this for Swift 3 anytime soon?

ATTENTION MAC SIERRA USERS:
You will not be able to complete this tutorial unless you head the (currently second to top) answer on stack: ios - APNs push notification with macOS Sierra - Stack Overflow

Basically Sierra updated php to 2.6, so you must replaced:

$streamContext = stream_context_create();

with:

$streamContext = stream_context_create([
            'ssl' => [
                'verify_peer'      => false,
                'verify_peer_name' => false
            ]
        ]);

or else you get this error:

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Users/USERNAME/APNS/newPush.php on line 24

Warning: stream_socket_client(): Failed to enable crypto in /Users/USERNAME APNS/newPush.php on line 24

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/USERNAME/APNS/newPush.php on line 24
Failed to connect: 0

Your solution worked like a charm.
Great !!! Thanks !!!