You are just one step away from creating awesome games!

Sign up with Google

You need a Google account to use Defold

- Select manual -

Push notifications

Push notifications are available on iOS and Android devices and allow your game to inform the player about changes and updates. The core functionality is similar between iOS and Android but there are some platform specific differences that you need to consider.

For a push notification to find its way from the server to the target device, certain bits of information are required for your app. The most complex part consists of security information that you set in the application so the notification system can verify the legitimacy of the client receiving notifications. But you will also need a piece of security information for your notification server so the Apple or Google servers can verify that your server is a legitimate notification sender. Finally, when you send notifications, you need to be able to uniquely direct notifications to a specific user’s device. For that you retreive and use a token that is unique to the particular device (i.e. user).

iOS setup

To get acquainted with the Apple Push Notification Service, a good idea is to start by reading Apple’s own documentation on how the service works. You can find it at https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

On iOS, you need the following information to send notifications:

  • Push Notifications must be enabled in the App ID.
  • A provisioning profile containing this valid App ID is also required.
  • You also need an Apple Push Notification Service SSL Certificate to be allowed to send notification data to the Apple Push Notification server from your messaging server application.

To get everything in place, head over to the Apple Developer Member center. Edit your AppID to enable Push Notifications.

AppID push notifications

You also need to create an Apple Push Notification service SSL certificate:

APN SSL certificate

The certificate will be needed on your server that will send out push notifications. While developing, you can download and install the certificate on your machine and run a push test app such as APNS-Pusher or NWPusher.

Make sure that you create a new provisioning profile from the AppID, and that you get it onto your device. You can do that manually from the “Member Center” page or through Xcode.

Provisioning profile

Note that it can take a while for Apple’s sandbox servers to update so you might not get push to work immediately. Be patient.

Now it’s time to run some test code:

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

function init(self)
    local sysinfo = sys.get_sys_info()
    if sysinfo.system_name == "Android" then
        msg.post("#", "push_android")
    elseif sysinfo.system_name == "iPhone OS" then
        msg.post("#", "push_ios")   

function on_message(self, message_id, message)
    if message_id == hash("push_ios") then
        push.register(alerts, function (self, token, error)
            if token then
                local t = ""
                for i = 1,#token do
                    t = t .. string.format("%02x", string.byte(token, i))
                -- Print the device token
                -- Error
    elseif message_id == hash("push_android") then
        push.register(nil, function (self, token, error)
            if token then
                -- Print the device token
                -- Error

If all goes well the notification listener will be registered and we get a token that we can use:

DEBUG:SCRIPT: 1f8ba7869b84b10df69a07aa623cd7f55f62bca22cef61b51fedac643ec61ad8

If you’re running a push test app, you can now try to send notifications to your device using the device token and the APN service SSL certificate.

Pusher test

The notification should arrive at the client soon after you send it, from within your test application, arriving to the function push_listener():

  aps = {
    badge = 42,
    alert = Testing.. (1),
    sound = default,

And from the iOS homescreen:

iOS notification

If you wish to update the badge count from within the application, use the push.set_badge_count() function.

Android setup

Google has extensive documentation for Google Cloud Messaging. We encourage you to start by reading it on https://developers.google.com/cloud-messaging/gcm

On Android, you need the following information to send notifications:

  • A GCM Sender ID. This is built into the application.
  • A Server API Key to enable sending notifications through Google’s servers.

The setup is quite straightforward. Start by heading over to http://developers.google.com, click on Android and then Google Cloud Messaging.

Android getting started

A bit down the page there is a button saying Get a configuration file.

Android configuration file

Click the button and follow the instructions. At the end you will get a Server API Key and a Sender ID.

Android cloud services info

Copy the Sender ID and paste it into the gcm_sender_id field in your Defold project settings.

Google Cloud Messaging sender ID

Now everything is ready on the client. The above code example works for Android as well. Run it and copy the device token id.

DEBUG:SCRIPT: APA91bHkcKm0QHAMUCEQ_Dlpq2gzset6vh0cz46kDDV6230C5rFivyWZMCxGXcjxRDKg1PK4z1kWg3xnUVqSDiO_4_RiG8b8HeYJfaoW1ho4ukWYXjq5RE0Sy-JTyrhqRusUP_BxRTcE

Now we have all information we need. Google’s notifications are sent through a Web API so we can use curl to send test messages:

$ curl  -X POST  -H "Content-type: application/json"  -H 'Authorization: key=SERVER_KEY' -d '{"registration_ids" : ["TOKEN_ID"], "data": {"alert": "Hello"}}' https://android.googleapis.com/gcm/send

Replace SERVER_KEY and TOKEN_ID with your specific keys.

Local push notifications

Local push notifications are supported as well as remote ones. After the regular setup you can 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, "A notification!", "Hello there", payload, { action = "get going" })

The id is uniquely identifying this scheduled notification and can be stored for later. The final parameter to push.schedule() takes a table with platform specific settings:


(iOS only). 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.


(iOS only). The numeric value of the icon badge. Set to 0 to clear the badge.


(Android only). The priority is a hint to the device UI about how the notification should be displayed. There are five priority levels:


Unless specified, the max priority level is used.

Inspecting scheduled notifications

The API provides two functions to inspect what is currently scheduled.

n = push.get_scheduled(id)

Which results in a table containing all details on the scheduled notification:

  payload = {"data":{"field":"Some value","field2":"Other value"}},
  title = A notification!,
  priority = 2,
  seconds = 19.991938,
  message = Hello there,

Note that seconds indicates the number of seconds left for the notification to fire. It is also possible to retreive a table with all scheduled notifications:

all_n = push.get_all_scheduled()

Which results in a table pairing notification id:s with their respective data:

  0 = {
    payload = {"data":{"field":"Some value","field2":"Other value"}},
    title = A notification!,
    priority = 2,
    seconds = 6.009774,
    message = Hey hey,
  1 = {
    payload = {"data":{"field":"Some value","field2":"Other value"}},
    title = Another notification!,
    priority = 2,
    seconds = 12.652521,
    message = Hello there,
  2 = {
    payload = {"data":{"field":"Some value","field2":"Other value"}},
    title = Hey, much notification!,
    priority = 2,
    seconds = 15.553719,
    message = Please answer!,

I want to report anonymously.