On this page

Mobile Push Notifications API for PHP SDK

The Mobile Push Notifications feature connects native PubNub publishing to third-party push services. Supported services include Google Android FCM (Firebase Cloud Messaging) and Apple iOS APNs (Apple Push Notification service).

To learn more, read about Mobile Push Notifications.

Add a device to a push notifications channel

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Enable mobile push notifications on a set of channels.

Method(s)

1$pubnub->addChannelsToPush()
2 ->pushType(PNPushType)
3 ->channels(array)
4 ->deviceId(string)
5 ->sync();
* required
ParameterDescription
pushType *
Type: PNPushType
Default:
Not set
Accepted values: PNPushType.GCM, PNPushType.APNS2.
channels *
Type: Array
Default:
n/a
Channels to enable for push notifications.
deviceId *
Type: String
Default:
n/a
Device identifier.
environment
Type: String
Default:
development
APNs environment. Required for APNS2.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required for APNS2.

Sample code

Add device to channel (FCM)

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
1

Add device to channel (APNS2)

1

List push notifications channels for a device

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

List channels that have push notifications enabled for the specified device token.

Method(s)

1$pubnub->listPushProvisions()
2 ->pushType(PNPushType)
3 ->deviceId(string)
4 ->sync();
* required
ParameterDescription
pushType *
Type: PNPushType
Default:
Not set
Accepted values: PNPushType.GCM, PNPushType.APNS2.
deviceId *
Type: String
Default:
n/a
Device token.
environment
Type: String
Default:
development
APNs environment. Required for APNS2.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required for APNS2.

Sample code

List channels for device

1

List channels for Device(APNS2)

1

Response

MethodDescription
getChannels()
Type: Array
List of channels associated for mobile push notifications.

Remove a device from push notifications channels

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable mobile push notifications on a set of channels.

Method(s)

1$pubnub->removeChannelsFromPush()
2 ->pushType(PNPushType)
3 ->channels(string|array)
4 ->deviceId(string)
5 ->sync();
* required
ParameterDescription
pushType *
Type: PNPushType
Default:
Not set
Accepted values: PNPushType.GCM, PNPushType.APNS2.
channels *
Type: String|Array
Default:
n/a
Channels to disable for push notifications.
deviceId *
Type: String
Default:
n/a
Device token.
environment
Type: String
Default:
development
APNs environment. Required for APNS2.
topic
Type: String
Default:
n/a
APNs topic (bundle identifier). Required for APNS2.

Sample code

Remove device from channel

1

Remove device from Channel(APNS2)

1

Remove a device from all push notifications channels

note
Requires Mobile Push Notifications add-on

Enable Mobile Push Notifications for your key in the Admin Portal. See how to enable add-on features.

Disable mobile push notifications from all channels registered with the specified device token.

Method(s)

1$pubnub->removeAllPushChannelsForDevice()
2 ->pushType(PNPushType)
3 ->deviceId(string)
4 ->sync();
* required
ParameterDescription
pushType *
Type: PNPushType
Default:
Not set
Accepted values: PNPushType.GCM, PNPushType.APNS2.
deviceId *
Type: String
Default:
n/a
Device token.

Sample code

Remove all push channels from device (FCM)

1

Response

The sync() method returns a response indicating the success or failure of the operation. This could be a status code, boolean, or detailed object, depending on the implementation.

1$response = $pubnub->removeAllPushChannelsForDevice()
2 ->pushType(PNPushType::APNS2)
3 ->deviceId("yourDeviceId")
4 ->sync();
5
6if ($response->isSuccessful()) {
7 echo "Successfully removed all push channels from device.";
8} else {
9 echo "Failed to remove push channels. Error: " . $response->getError();
10}
Last updated on