apns / apnsphp
A Library for Apple Push Notification Service
dev-master
2018-03-31 17:44 UTC
This package is not auto-updated.
Last update: 2022-09-12 05:40:48 UTC
README
A PHP Library for Apple Push Notification Service
Features
- Send Push Notification
- Customise as JSON Payload
Pre-requisite
- Device Token which you will get as callback (in AppDelegate.swift) when you register for Push Notification.
- PEM Certificate of your app which you get from Apple Developer Portal.
Installation
You can install apnsPHP via Composer. First, add composer.json file in your project
{ "require": { "apns/apnsPHP": "*" } }
And, install dependencies by command
composer install
Code
- Send Push Notification when you've text only
I'm assuming you have pem certificate with you.
//Path of Certfiicate $pathOfCertificate = $_SERVER['DOCUMENT_ROOT']."/certificate.pem"; // Initialise APNS $apns = new apnsPHP($pathOfCertificate); $apns->sendPushNotification("This is my Message", $token);
- Send Push Notification when you've complete JSON Payload
As, Apple Push Notification payload looks like this where, alert is the push-notification message, badge is the badge number which comes on AppIcon in numeric form with red bubble and last is sound which is alert tone when push notification pops.
{ "aps":{ "alert" : "Hello World :D", "badge" : 1, "sound" : "default" } }
So, you have to first prepare the payload and then call apns defined method,
// Token $token = "FCFFA7C61D647BA62DAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Associative Array $payload['aps'] = array('alert' => 'Hello World :D', 'badge' => 1, 'sound' => 'default'); // Convert Array into JSON $payloadJSON = json_encode($payload);
And, last you have to call APNS method.
$apns = new apnsPHP($pathOfCertificate); $apns->sendPushNotification($payloadJSON, $token);
Reference
You can refer Apple Documentation for Apple Push Notification Service.