Part1 describes how to setup OneSignal plugin with Corona SDK. Part2 API list. Part3 to integrate with Kwik4
Part 1 - OneSignal Corona SDK Push Notification Plugin Example
OneSignal is a free push notification service for mobile apps. They've partnered with Corona SDK to make it easy to use OneSignal in your Corona Apps. Simply follow our directions to get started.
setup
build.settings
settings = { orientation = { default = "portrait", supported = { "portrait", "portraitUpsideDown" } }, iphone = { plist = { UIBackgroundModes = {"remote-notification"}, UIStatusBarHidden = false, UIPrerenderedIcon = true, -- set to false for "shine" overlay } }, plugins = { ["plugin.OneSignal"] = { publisherId = "com.onesignal" }, }, }
main.lua
local OneSignal = require("plugin.OneSignal")
receive notificaton
function DidReceiveRemoteNotification(message, additionalData, isActive) native.showAlert("message:", message, { "OK" } ) end
- message is message of OneSignal
- addionalData is a key/value pair of OneSignal message
- isActive
- true if app is in active foreground
- false if app is in background or not running when a notification is received
init
replace the following id&number with yours
- OneSignal AppID: b2f7f966-d8cc-11e4-bed1-df8f05be55ba
- Google Project number (for Android) :703322744261
OneSignal.Init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba", "703322744261", DidReceiveRemoteNotification)
Butoon - Get Ids
pressing the button to enable user to receive Notification
OneSignal.DisableAutoRegister() OneSignal.RegisterForNotifications()
check
OneSignal.IdsAvailableCallback( function (userId, pushToken) print("userId:" .. userId) if (pushToken) then print("pushToken:" .. pushToken) else native.showAlert("error", "push token is nil", { "OK" } ) end end)
if pushuToken is nil , there was a connection issue or on iOS notification permissions were not accepted.
- does the provisioning profile contains push?
- app is built with the provisining profile
- is the provisioning profile installed in the real device?
Test
send a push notification from OneSignal while your app is under each state of
- Not ruuning
- Backgronded
Forgrounded
Part 2 - OneSignal API supported
Part1 descirbed a basic setting to receive a push notification.
You would like to use these OneSignla APIs
- ClearAllNotifications function
- SetSubscription function to control on/off notifications
moreover
- PromptLocation function to get a user's permission for location and send the location info to OneSignal
- SendTag function
your may post a nofication via OneSignal to others by * PostNotification function
Complete list of all the APIs
OneSinal plugin supports the following functions - SDK Document
- init(callback)
- callback(message, additionalData, isActive)
- DisableAutoRegister()
- ~~kOSSettingsKeyInAppLaunchURL~~
- RegisterForNotifications()
- IdsAvailableCallback(callback)
- callback(userID, pushToken)
- GetTags(callback)
- callback(tags)
- SendTag(key, value)
- SendTags(keyValuePairs)
- DeleteTag(key)
- DeleteTags(keys)
- PromptLocation()
- build.settings plist
- NSLocationUsageDescription
- NSLocationWhenInUseUsageDescription
- build.settings plist
- PostNotification(notification(notification, onSuccess, onError)
- onSuccess(json)
- onError(json)
- ~~cancelNotification~~
- ClearAllNotifications()
SetSubscription(enable)
~~OSHandleNotificationReceivedBlock~~
- ~~OSNotificationOpenedResult~~
- ~~OSNotification~~
- ~~OSNotificationAction~~
- ~~OSNotificationDisplayType~~
- ~~OSNotificationPayload~~
Android * EnableVibrate(enable) * EnableSound(enable)
Debug * SetLogLevel(logLevel, visualLevel)
Part 3 - External Library and Code - Kwik
please update Kwik4 to the latest upate - 2018.1109 or later
add the plugin to build.settings as described in Part1
- tmplt/build.settings or build4/build.settings
Projects&Pages > Add External Library Name: OneSignal Path: plugin.OneSignal
Create Variable
- Name: notifiedMessage as global
- Name: enableSubscription
Add Button with external code
- Name: register
- onsignal_register.lua
- Name: clear
- onsignal_clearAllNotification.lua
- Name: setSubs
- setSubscription.lua
- Name: register
Add Action
- Name: notifiedAction
Add Main.lua External Code
- onesignal_init.lua
function DidReceiveRemoteNotification(message, additionalData, isActive) native.showAlert("message:", message, { "OK" } ) _K.notifiedMessage = message end OneSignal.DisableAutoRegister() OneSignal.Init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba", "703322744261", DidReceiveRemoteNotification) OneSignal.EnableInAppAlertNotification(true)
Add External Code
- onsignal_register.lua for button action
OneSignal.RegisterForNotifications() OneSignal.IdsAvailableCallback( function (userId, pushToken) print("userId:" .. userId) if (pushToken) then print("pushToken:" .. pushToken) end end)
- onsignal_clearAllNotification.lua for button action
OneSignal.ClearAllNotifications()
- onsignal_setSubscription.lua for button action
local enable = UI.enableSubscription OneSignal.SetSubscription(enable)
Note
if you want to display message in a text layer of a page instead of native.showAlert, it can be done with associating notifiedMessage variable to a dynamic text replacement.
when a message is received, open the page with showOverlay function
Dynamic Text
- Name: msgTxt
- associate with notifiedMessage
Add Button
- Name: hideBtn
- external code close_overlay.lua
however, push notification comes anytime, it would be a trouble when the page with the dynamic text is not loaded yet. You may open the page by composer.showOverlay function. For instance, if you want to show the pushed message on page1, use the init code below
onesignal_init.lua for main.lua
local composer = require("composer") function DidReceiveRemoteNotification(message, additionalData, isActive) _K.notifiedMessage = message composer.showOverlay("views.page01Scene") end OneSignal.DisableAutoRegister() OneSignal.Init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba", "703322744261", DidReceiveRemoteNotification) OneSignal.EnableInAppAlertNotification(true)
close_overlay.lua
local composer = require("composer") composer.hideOverlay()
Bookshelf Embedded project
- copy the lines of onesignal_init.lua to main.lua.
- If you use composer.showOverlay, TOC and each book must have a page for displaying a message text and closing button of hydeOvelay
- you setup a setting page of notification in a project as adding plugin.OneSignal as a library described above
- add the plugin to build.settings as described in Part1
├── App │ ├── TOC │ ├── book01 │ └── book02 ├── build.settings ├── config.lua ├── extlib ├── lib └── main.lua
https://docs.coronalabs.com/api/library/composer/showOverlay.html