Difference between revisions of "NotificationManager"
Line 8: | Line 8: | ||
NotificationManager instance also receives events about notification being clicked and app opened through that specific notification. As well as registration for push notification event or registration error. | NotificationManager instance also receives events about notification being clicked and app opened through that specific notification. As well as registration for push notification event or registration error. | ||
+ | === Examples === | ||
+ | '''Modifying existing scheduled notification'''<br/> | ||
+ | <source lang="lua">--id of notification to modify | ||
+ | local id = 1 | ||
+ | |||
+ | --retrieve shared instance | ||
+ | local mngr = NotificationManager.getSharedInstance() | ||
+ | |||
+ | --retrieve table with scheduled notifications | ||
+ | local t = mngr:getScheduledNotifications() | ||
+ | |||
+ | --check if id is in it | ||
+ | if t[id] then | ||
+ | --notification is still scheduled | ||
+ | --let's modify it by creating new instance with same id | ||
+ | local note = Notification.new(id) | ||
+ | note:setTitle("New title") | ||
+ | note:setMessage("New message") | ||
+ | --if we want to modify dispatch time we need to redispatch it | ||
+ | note:dispatchAfter({day = 1}) | ||
+ | end</source> | ||
+ | '''Checking if notification is stil scheduled'''<br/> | ||
+ | <source lang="lua">--id of notification to check | ||
+ | local id = 1 | ||
+ | |||
+ | --retrieve shared instance | ||
+ | local mngr = NotificationManager.getSharedInstance() | ||
+ | |||
+ | --retrieve table with scheduled notifications | ||
+ | local t = mngr:getScheduledNotifications() | ||
+ | |||
+ | --check if id is in it | ||
+ | if t[id] then | ||
+ | --notification is still scheduled | ||
+ | end</source> | ||
+ | '''Catching event if app opened from local notification or notification received in background'''<br/> | ||
+ | <source lang="lua">--retrieve shared instance | ||
+ | local mngr = NotificationManager.getSharedInstance() | ||
+ | |||
+ | mngr:addEventListener(Event.LOCAL_NOTIFICATION, function(e) | ||
+ | --getting id of notification | ||
+ | local id = e.id | ||
+ | |||
+ | --printing other information | ||
+ | print("Title: "..e.title) | ||
+ | print("Text: "..e.message) | ||
+ | print("Number: "..e.number) | ||
+ | end)</source> | ||
+ | '''Registering for push notifications'''<br/> | ||
+ | <source lang="lua">--retrieve shared instance | ||
+ | local mngr = NotificationManager.getSharedInstance() | ||
+ | |||
+ | --if registration completed succesfully | ||
+ | mngr:addEventListener(Event.PUSH_REGISTRATION, function(e) | ||
+ | --getting device token | ||
+ | local token = e.deviceToken | ||
+ | |||
+ | --sending token to your server | ||
+ | local loader = UrlLoader.new("http://yourdomain.com/register.php?token="..token) | ||
+ | loader:addEventListener(Event.COMPLETE, function() | ||
+ | --token succesfuly deliverd | ||
+ | end) | ||
+ | end) | ||
+ | |||
+ | --if registration failed | ||
+ | mngr:addEventListener(Event.PUSH_REGISTRATION_ERROR, function(e) | ||
+ | --device could not been registered now | ||
+ | --try again later | ||
+ | print(e.error) | ||
+ | end) | ||
+ | |||
+ | |||
+ | --try to register for push notifications | ||
+ | mngr:registerForPushNotifications("953841987672")</source> | ||
{|- | {|- | ||
| style="width: 50%;"| | | style="width: 50%;"| |
Revision as of 10:44, 23 August 2018
Supported platforms:
Available since: Gideros 2011.6
Description
NotificationManager is the class that allows you to manage scheduled and received notifications. This is a class with a single instance which can be retrieved using getSharedInstance() and can not have any new instances.
With NotificationManager you can retrieve information about all scheduled local notifications, all received local notifications and all received push notifications. As well as clear all type of notifications and cancel scheduled notifications.
NotificationManager instance also receives events about notification being clicked and app opened through that specific notification. As well as registration for push notification event or registration error.
Examples
Modifying existing scheduled notification
--id of notification to modify
local id = 1
--retrieve shared instance
local mngr = NotificationManager.getSharedInstance()
--retrieve table with scheduled notifications
local t = mngr:getScheduledNotifications()
--check if id is in it
if t[id] then
--notification is still scheduled
--let's modify it by creating new instance with same id
local note = Notification.new(id)
note:setTitle("New title")
note:setMessage("New message")
--if we want to modify dispatch time we need to redispatch it
note:dispatchAfter({day = 1})
end
Checking if notification is stil scheduled
--id of notification to check
local id = 1
--retrieve shared instance
local mngr = NotificationManager.getSharedInstance()
--retrieve table with scheduled notifications
local t = mngr:getScheduledNotifications()
--check if id is in it
if t[id] then
--notification is still scheduled
end
Catching event if app opened from local notification or notification received in background
--retrieve shared instance
local mngr = NotificationManager.getSharedInstance()
mngr:addEventListener(Event.LOCAL_NOTIFICATION, function(e)
--getting id of notification
local id = e.id
--printing other information
print("Title: "..e.title)
print("Text: "..e.message)
print("Number: "..e.number)
end)
Registering for push notifications
--retrieve shared instance
local mngr = NotificationManager.getSharedInstance()
--if registration completed succesfully
mngr:addEventListener(Event.PUSH_REGISTRATION, function(e)
--getting device token
local token = e.deviceToken
--sending token to your server
local loader = UrlLoader.new("http://yourdomain.com/register.php?token="..token)
loader:addEventListener(Event.COMPLETE, function()
--token succesfuly deliverd
end)
end)
--if registration failed
mngr:addEventListener(Event.PUSH_REGISTRATION_ERROR, function(e)
--device could not been registered now
--try again later
print(e.error)
end)
--try to register for push notifications
mngr:registerForPushNotifications("953841987672")
MethodsNotificationManager.getSharedInstance - get NotificationManager instance |
EventsEvent.LOCAL_NOTIFICATION Constants |