PHP script to send Android, iOS and Windwos Phone push notification

php-script-push-notification-in-android-ios-windows
Simple setup of Push notification php script for android, ios and windows phone 8 devices. Push notifications are great for keeping users informed with timely and relevant content, whether your app is running in the background or inactive. Notifications can display a message, play a distinctive sound, or update a badge on your app icon.

Android devices receive push notifications through the Google Cloud Messaging (GCM) service , whereas iOS devices receive them from the Apple Push Notifications (APN) Service and Windows Phone 8 devices receive them from the Microsoft Push Notification Service (MPNS). Though there are differences in the three services in terms of the size of the payload that can be sent, certificates required etc.

All three act as a store and forward type of service where they receive a message from a 3rd party, identify the recipient and pass it along. Upon receipt, your application that has registered to receive them can examine the returned contents and act accordingly. For more on each of the service, read their respective documentation.

 

Android Pre-requisites
Google Cloud Messaging Project ID
Google Cloud Messaging API Key for above Project ID (need for server)

iOS Pre-requisites
App ID configured for Push Notifications on Apple Developer Portal
SSL Certificate and private key (need for server)

WP8 Pre-requisites
The name of our push channel

 

I have worked on cordova / phonegap apps and I’m using push notifications for all the major platforms. So, thought of creating this useful script which is highly configurable and easy to use. The cordova push notification plugin, I have been using. And, I have created a Gist on Github of the php script to send push notifications to various devices.

  • Pavel Zhuravlev

    Hi Joash!
    If I need to send notification to 1000 devices I need to do 1000 curl requests?

    • You can just use a foreach loop and call the required platform specific notification. Let me know, if you need an example.

      • Pavel Zhuravlev

        yes, please

      • Dave Gold

        Hello, could you please email me the example as well. This could come in handy. BTW thank you for this tut. its better than any other I found so far especially given it supports all 3 platforms.

      • Dante Bland

        May I get one as well?

  • Andra Madianata

    I am very keen to make android app. But there are things that still make me doubt, that the notification sound.
    I created a web-based conversation, such as WhatsApp or BBM, if there is a new message that comes into the data base of the voice notification will appear. This time I want to ask how to display the sound for notification? Whether from android app or on the website?
    Thank you.

  • Yelitza Cintrón

    Hi!!! 🙂 Joash! 🙂 This is great!!! 🙂 But, how do I install it? I put it on my server, but when open the PHP nothing happens!! 🙁

    • You just have to run TestNotification.php
      Hope you have edited/replaced the parameters based on device accordingly. May I know, for which platform are your testing?

  • Paresh

    Hi Joash ,

    I am trying to send push notification to iOS but it gives me the error

    Failed to connect: 0 .

    Please have your feedback

    Thanks

    • Hi Paresh,
      There could be many reasons for this.
      First check if you have created a valid ck.pem file(certificate).
      Secondly try connecting to gateway.sandbox.push.apple.com using telnet via terminal.
      Thirdly make sure ck.pem and the php file are in same folder. Else set an absolute path for the certificate file in php.

      If your still facing issues, Let me know.

  • Dave Gold

    Hello, this looks very nice.

    One thing I am not sure about is. How to obtain the passphrase for use for iOS? What should the passphrase look like?

    I didnt find any answers on google for this.

    • Hi Dave,

      When you are creating the .pem file using openssl for the certificate you will be asked to enter a passphrase. Passphrase is nothing but a password for your encryption.

      Example: http://pastebin.com/xC4vdMmx

      • Dave Gold

        Oh I see… Thank you.

        What are the risks of not setting any passphrase? Some tutorials state to leave it blank for some reason. Dont say why though.

        • If you want to protect the p12 file with a private key. You need a passphrase. I recommend on creating one. So, that the cert is secure.

  • Rohit Suthar

    Hi joashp,

    I’m getting NetworkError: 500 Internal Server Error when i’m execute TestNotifications.php

    Please help to resolve my issue. thanks!

    • HI Rohit,
      This could be for many reasons.
      Mainly check for permission of the file or the .htacsess file.

      • Rohit Suthar

        Thanks for the reply… but i don’t have any .htaccess file & also don’t have any permission issue.

        • Hi Rohit,
          Try debugging it remotely, to see what might be the problem.
          If you are still having problem share me what you are doing via email.

          • yarvit0

            Same error, how did you fix it?

          • Check, if code has fatal errors, enable errors on server temporally and check.

  • melvin gomez

    Hi Josh,Pushnotification is not working when i changed my server with php version 5.3.3(earlier 5.4.45)

    • Hi Melvin,
      This should not be an issue. Did you check server logs for any known errors?

  • Michael Angelo

    Thanks for this!

    I’m receiving an error that says: “Strict Standards. Non-static method PushNotifications::android should not be called statically”.

    I’m also receiving another one that says: “Fatal error. Using $this when not in object context”.

    Would you happen to have any suggestions to correct these two errors?