iOS Publishing Notifications

Push notifications are triggered by your servers to our Beams service. After a device using your iOS application subscribes to an interest on our service, your server can then send a push notification to that device by publishing to that interest.

Before you can send push notifications, make sure you have configured your APNs Key andintegrated your app with the SDK.

Sending a Notification

To get started, let's send the time-honored “hello world” message to your iOS application. We'll assume your iOS application has subscribed to the interest "hello".

Let's start by creating a file called publish-body.json with the request body for the publish:

JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "interests": [
    "hello"
  ],
  "apns": {
    "aps": {
      "alert": {
        "title": "Hello",
        "body": "Hello, world!"
      }
    }
  }
}

Now, before you run the curl command, you need to get the instance id and its secret. You can get this information in the Pusher Dashboard "Keys" tab for your Beams instance. Now you can either export environment variables or replace the command with the appropriate keys.

bash

1
2
3
curl -H "Content-Type: application/json" \
     -H "Authorization: Bearer $SECRET_KEY" \
     -X POST "https://$INSTANCE_ID.pushnotifications.pusher.com/publish_api/v1/instances/$INSTANCE_ID/publishes" \

From this point onwards, you can just change the publish-body.json file and run the same curl command.

Sending From the Server

  • PHP
  • Go
  • Node.js
  • Python
  • Java
  • Kotlin
  • Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$pushNotifications = new \Pusher\PushNotifications\PushNotifications(array(
  "instanceId" => "YOUR_INSTANCE_ID_HERE",
  "secretKey" => "YOUR_SECRET_KEY_HERE",
));

$publishResponse = $pushNotifications->publish(
  array("hello"),
  array("apns" => array("aps" => array(
    "alert" => array(
      "title" => "Hello",
      "body" => "Hello, World!",
    ),
  )),
));

Advanced Options

The publish method accepts an object which specifies the message to send. This object is the language's native representation of JSON. The previous example was a very simple one. There are many more advanced details you can specify. The full set of options for the APNs section of the publish call is described in Apple's Payload Key Reference. For further examples, see Apple's Creating the Remote Notification Payload.

If you encounter error responses from the Beams service, you may wish to consult Apple's push notification troubleshooting guide.

Custom Data

Any custom data fields you wish to include in your push notification must be provided under a data key in the apns data structure. Any custom data fields specified outside of the data key won't be pushed to the device.

Did you find this document useful?

We are always striving to create the most accurate and informative docs as possible. If there is something especially wrong (or right) here then please let us know.