Adding Local Notifications to Your Corona SDK App

In the app world, you can use notifications to bring players back to your game by reminding them of events or rewarding them using a daily rewards system. Corona SDK happens to support local notifications and push notifications.

Local notifications are setup in the device and sent to the player by the same device. Push notifications are scheduled and sent through a remote server. This tutorial is going to focus on local notifications only. And, it’s surprisingly easy.

A note from Corona: Notifications are only supported on iOS and Android, not on macOS desktop, Windows desktop, Apple TV, or Android TV. You can read the full documentation at https://docs.coronalabs.com/guide/events/appNotification/index.html.

To start things off, add in the plugin for notifications to your build.settings file.

["plugin.notifications.v2"] =
{
 publisherId = "com.coronalabs"
},

This plugin can be added along with the other plugins you may already have in your code. After build.settings, you are going to need to add in what type of notification you are using in config.lua. You can choose to add a badge, a sound, or an alert. If you’re just bring players back to your game, then an alert might suffice. Here’s the code for your config.lua file.

application =
{
 notification =
 {
  iphone =
  {
   types =
   {
    "badge",
    "sound",
    "alert"
   },
  },
 },
}

For Android, you can set a custom notification icon. For my apps, I tend to use the same icon as the app icon for simplicity. Here’s the list of icon names and their sizes for Android.

File, Size (w×h)

IconNotificationDefault-ldpi.png,       18 × 18

IconNotificationDefault-mdpi.png,     24 × 24

IconNotificationDefault-hdpi.png,      36 × 36

IconNotificationDefault-xhdpi.png, 48 × 48

IconNotificationDefault-xxhdpi.png, 72 × 72

IconNotificationDefault-xxxhdpi.png, 96 × 96

Once build.settings, config.lua, and your icons are set, you can add the library to your app by using the handy require() call.

local notifications = require( "plugin.notifications.v2" )

I would recommend that you add this code in the title screen of your game or after notifying the player what the notifications are for (more on that later). I haven’t personally tried it, but main.lua doesn’t seem like it’s the right fit to add notifications to.

Now, we’re ready for the notifications. If you want to keep things simple, like Corona, then this is all you need to send notifications to your user.

-- Set up notification options

local options = {

alert = "Play What The Rebus to earn 30 brand new coins!",

}

-- Schedule a notification to occur 24 hours from now

local notification1 = notifications.scheduleNotification( 43200, options )

The options table contains the alert property with a custom string. The notifications.scheudleNotification() creates a notification for the app and in 24 hours, an alert will be played to the user. And that’s it! You now have a simple alert notification that lets the player know to return to your app.

If you wanted to add a badge icon or play a sound, you can do so by adding in the badge property or the sound property. The sound, alarm.caf, can be renamed to the sound you have in the root of your project. In the example below, you are setting a notification for an alert, badge, and sound at the same time.

-- Set up notification options

local options = {

 alert = "Play What The Rebus to earn 30 brand new coins!",

 badge = 1,

 sound = "alarm.caf"

}

-- Schedule a notification to occur 24 hours from now

local notification1 = notifications.scheduleNotification( 43200, options )

For the power users out there, this may not be enough. If it isn’t, don’t worry because Corona has you covered. You can set custom properties in your options table and set your app to respond to the notification. Here’s the full code to place in your project (this was copied directly from Corona documentation because it’s a great example).

 

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here
local notifications = require( "plugin.notifications.v2" )

-- Get the app's launch arguments in case it was started when the user tapped on a notification
local launchArgs = ...

-- Set up notification options
local options = {
    alert = "Play What The Rebus to earn 30 brand new coins!",
    badge = 1,
    sound = "alarm.caf",
    custom = { foo = "bar" }
}

-- Schedule a notification to occur 24 hours from now
local notification1 = notifications.scheduleNotification( 43200, options )

-- Listen for notifications
local function onNotification( event )
    print( event.name )
    if ( event.custom ) then
        print( event.custom.foo )
    end
end
Runtime:addEventListener( "notification", onNotification )

-- The launch arguments provide a notification event if this app was started when the user tapped on a notification
-- In this case, you must call the notification listener manually
if ( launchArgs and launchArgs.notification ) then
    onNotification( launchArgs.notification )
end

In the example above, you are setting an alert, a badge, and a sound notification with a custom property called foo with the value of bar. When the app is opened up via notification or opened after the player has received a notification, the statement print(event.custom.foo) will run. In place of this print statement, you can place your custom code to respond to notifications like rewarding the player with coins or unlocking more health. If you’re using badges, this would be a good place to place native.setProperty( “applicationIconBadgeNumber”, 0 ). This statement clears the badge icon on iOS devices. If you are handling more than one badge, refer to this part of Corona’s documentation.

To wrap up this tutorial, I would recommend that you display a message to the player before prompting them to allow for notifications. My guess is that today’s players leave off notifications, especially when they don’t know what they will be doing. By using a simple popup stating why you are asking for notifications will help get you more people using your notifications.

If you have questions, leave them in the comments below. Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *