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)

$pubnub->addChannelsToPush()
->pushType(PNPushType)
->channels(array)
->deviceId(string)
->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.

Add device to channel (APNS2)


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)

$pubnub->listPushProvisions()
->pushType(PNPushType)
->deviceId(string)
->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


List channels for Device(APNS2)


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)

$pubnub->removeChannelsFromPush()
->pushType(PNPushType)
->channels(string|array)
->deviceId(string)
->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


Remove device from Channel(APNS2)


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)

$pubnub->removeAllPushChannelsForDevice()
->pushType(PNPushType)
->deviceId(string)
->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)


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.

$response = $pubnub->removeAllPushChannelsForDevice()
->pushType(PNPushType::APNS2)
->deviceId("yourDeviceId")
->sync();

if ($response->isSuccessful()) {
echo "Successfully removed all push channels from device.";
} else {
echo "Failed to remove push channels. Error: " . $response->getError();
}
Last updated on