Defold Push API documentation

Functions for interacting with push notifications. Supported on iOS and Android.

Usage

To use this library in your Defold project, add the following URL to your game.project dependencies:

https://github.com/defold/extension-push/archive/master.zip

We recommend using a link to a zip file of a specific release.

Dependencies

Defold 1.2.164 and below does not use Gradle. You need to add the following dependencies to your game.project file:

https://github.com/defold/android-base-extensions/releases/download/1.0.0/firebase-core-16.0.8.zip
https://github.com/defold/android-base-extensions/releases/download/1.0.0/firebase-messaging-17.3.4.zip
https://github.com/defold/android-base-extensions/releases/download/1.0.0/gps-base-16.0.1.zip
https://github.com/defold/android-base-extensions/releases/download/1.0.0/gps-measurement-16.4.0.zip
https://github.com/defold/android-base-extensions/releases/download/1.0.0/support-v4-26.1.0.zip

Defold 1.2.165 and above uses Gradle to resolve dependencies. This means that there is no need to add any additional library projects to your game.project file.

Source code

The source code is available on GitHub

API reference

Modules

push

Functions and constants for interacting with local, as well as Apple’’s and Google’’s push notification services. These API’s only exist on mobile platforms. [icon:ios] [icon:android]

Enums

push.NOTIFICATION_BADGE

Badge notification type.

push.NOTIFICATION_SOUND

Sound notification type.

push.NOTIFICATION_ALERT

Alert notification type.

push.ORIGIN_LOCAL

Local push origin.

push.ORIGIN_REMOTE

Remote push origin.

push.PRIORITY_MIN

This priority is for items might not be shown to the user except under special circumstances, such as detailed notification logs. Only available on Android. [icon:android]

push.PRIORITY_LOW

Priority for items that are less important. Only available on Android. [icon:android]

push.PRIORITY_DEFAULT

The default notification priority. Only available on Android. [icon:android]

push.PRIORITY_HIGH

Priority for more important notifications or alerts. Only available on Android. [icon:android]

push.PRIORITY_MAX

Set this priority for your application’s most important items that require the user’s prompt attention or input. Only available on Android. [icon:android]


Functions

push.register() Send a request for push notifications. Note that the notifications table para...
push.set_listener() Sets a listener function to listen to push notifications.
push.set_badge_count() Set the badge count for application icon. This function is only available on ...
push.schedule() Local push notifications are scheduled with this function. The returned `id` ...
push.cancel() Use this function to cancel a previously scheduled local push notification. T...
push.get_scheduled() Returns a table with all data associated with a specified local push notifica...
push.get_all_scheduled() Returns a table with all data associated with all scheduled local push notifi...

push.register(notifications, callback)

Parameter Type Description
notifications table

The types of notifications to listen to. [icon:ios]

callback function

Register callback function.

Parameter Type Description
self object

The current object.

token string

The returned push token if registration is successful.

error table

A table containing eventual error information.

Send a request for push notifications. Note that the notifications table parameter is iOS only and will be ignored on Android.

Examples

Register for push notifications on iOS. Note that the token needs to be converted on this platform.

local function push_listener(self, payload, origin)
     -- The payload arrives here.
end

function init(self)
     local alerts = {push.NOTIFICATION_BADGE, push.NOTIFICATION_SOUND, push.NOTIFICATION_ALERT}
     push.register(alerts, function (self, token, error)
     if token then
          -- NOTE: %02x to pad byte with leading zero
          local token_string = ""
          for i = 1,#token do
              token_string = token_string .. string.format("%02x", string.byte(token, i))
          end
          print(token_string)
          push.set_listener(push_listener)
     else
          -- Push registration failed.
          print(error.error)
     end
end

Register for push notifications on Android.

local function push_listener(self, payload, origin)
     -- The payload arrives here.
end

function init(self)
     push.register({}, function (self, token, error)
         if token then
              print(token)
              push.set_listener(push_listener)
         else
              -- Push registration failed.
              print(error.error)
         end
    end)
end

push.set_listener(listener)

Parameter Type Description
listener function

Listener callback function.

Parameter Type Description
self object

The current object.

payload table

The push payload

origin constant

Origin of the push that can be one of the predefined constants below

  • push.ORIGIN_LOCAL
  • push.ORIGIN_REMOTE
activated boolean

If the application was activated via the notification.

Sets a listener function to listen to push notifications.

Examples

Set the push notification listener.

local function push_listener(self, payload, origin, activated)
     -- The payload arrives here.
     pprint(payload)
     if origin == push.ORIGIN_LOCAL then
         -- This was a local push
         ...
     end

     if origin == push.ORIGIN_REMOTE then
         -- This was a remote push
         ...
     end
end

local init(self)
     ...
     -- Assuming that push.register() has been successfully called earlier
     push.set_listener(push_listener)
end

push.set_badge_count(count)

Parameter Type Description
count number

Badge count

Set the badge count for application icon. This function is only available on iOS. [icon:ios]

push.schedule(time, title, alert, payload, notification_settings)

Parameter Type Description
time number

Number of seconds into the future until the notification should be triggered.

title string

Localized title to be displayed to the user if the application is not running.

alert string

Localized body message of the notification to be displayed to the user if the application is not running.

payload string

JSON string to be passed to the registered listener function.

notification_settings table

Table with notification and platform specific fields

  • action string -

    The alert action string to be used as the title of the right button of the alert or the value of the unlock slider, where the value replaces “unlock” in “slide to unlock” text. [icon:ios]

  • badge_count number -

    The numeric value of the icon badge. [icon:ios]

  • priority number -

    The priority is a hint to the device UI about how the notification should be displayed. There are five priority levels, from -2 to 2 where -1 is the lowest priority and 2 the highest. Unless specified, a default priority level of 2 is used. [icon:android]

Returns

Return value Type Description
number

Unique id that can be used to cancel or inspect the notification

string

Error string if something went wrong, otherwise nil

Local push notifications are scheduled with this function. The returned id value is uniquely identifying the scheduled notification and can be stored for later reference.

Examples

This example demonstrates how to schedule a local notification:

-- Schedule a local push in 3 seconds
local payload = '{ "data" : { "field" : "Some value", "field2" : "Other value" } }'
id, err = push.schedule(3, "Update!", "There are new stuff in the app", payload, { action = "check it out" })
if err then
     -- Something went wrong
     ...
end

push.cancel(id)

Parameter Type Description
id number

The numeric id of the local push notification

Use this function to cancel a previously scheduled local push notification. The notification is identified by a numeric id as returned by push.schedule().

push.get_scheduled(id)

Parameter Type Description
id number

The numeric id of the local push notification.

Returns

Return value Type Description
table

Table with all data associated with the notification.

Returns a table with all data associated with a specified local push notification. The notification is identified by a numeric id as returned by push.schedule().

push.get_all_scheduled()

Returns

Return value Type Description
table

Table with all data associated with all scheduled notifications.

Returns a table with all data associated with all scheduled local push notifications. The table contains key, value pairs where the key is the push notification id and the value is a table with the notification data, corresponding to the data given by push.get_scheduled(id).