Difference between revisions of "NotificationManager"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:NotificationManager -->
 
<!-- GIDEROSOBJ:NotificationManager -->
'''<translate>Supported platforms</translate>:''' <br/>
+
'''Supported platforms:''' <br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/EventDispatcher|EventDispatcher]]<br/>
+
'''Inherits from:''' [[EventDispatcher]]<br/>
=== <translate>Description</translate> ===
+
 
<translate>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.
+
=== 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.
 
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.</translate>
+
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.
=== <translate>Examples</translate> ===
+
 
'''Modifying existing scheduled notification'''<br/>
+
=== Examples ===
<syntaxhighlight lang="lua">--id of notification to modify
+
'''Modifying existing scheduled notification'''
 +
<syntaxhighlight lang="lua">
 +
--id of notification to modify
 
local id = 1
 
local id = 1
  
Line 31: Line 33:
 
--if we want to modify dispatch time we need to redispatch it
 
--if we want to modify dispatch time we need to redispatch it
 
note:dispatchAfter({day = 1})
 
note:dispatchAfter({day = 1})
end</syntaxhighlight>
+
end
'''Checking if notification is stil scheduled'''<br/>
+
</syntaxhighlight>
<syntaxhighlight lang="lua">--id of notification to check
+
 
 +
'''Checking if notification is stil scheduled'''
 +
<syntaxhighlight lang="lua">
 +
--id of notification to check
 
local id = 1
 
local id = 1
  
Line 45: Line 50:
 
if t[id] then
 
if t[id] then
 
--notification is still scheduled
 
--notification is still scheduled
end</syntaxhighlight>
+
end
'''Catching event if app opened from local notification or notification received in background'''<br/>
+
</syntaxhighlight>
<syntaxhighlight lang="lua">--retrieve shared instance
+
 
 +
'''Catching event if app opened from local notification or notification received in background'''
 +
<syntaxhighlight lang="lua">
 +
--retrieve shared instance
 
local mngr = NotificationManager.getSharedInstance()
 
local mngr = NotificationManager.getSharedInstance()
  
Line 58: Line 66:
 
print("Text: "..e.message)
 
print("Text: "..e.message)
 
print("Number: "..e.number)
 
print("Number: "..e.number)
end)</syntaxhighlight>
+
end)
'''Registering for push notifications'''<br/>
+
</syntaxhighlight>
<syntaxhighlight lang="lua">--retrieve shared instance
+
 
 +
'''Registering for push notifications'''
 +
<syntaxhighlight lang="lua">
 +
--retrieve shared instance
 
local mngr = NotificationManager.getSharedInstance()
 
local mngr = NotificationManager.getSharedInstance()
  
Line 82: Line 93:
 
end)
 
end)
  
 +
--try to register for push notifications
 +
mngr:registerForPushNotifications("953841987672")
 +
</syntaxhighlight>
  
--try to register for push notifications
 
mngr:registerForPushNotifications("953841987672")</syntaxhighlight>
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/NotificationManager.getSharedInstance|NotificationManager.getSharedInstance]] ''<translate>get NotificationManager instance</translate>''<br/><!-- GIDEROSMTD:NotificationManager.getSharedInstance() get NotificationManager instance -->
+
[[NotificationManager.getSharedInstance]] ''gets NotificationManager instance''<br/><!--GIDEROSMTD:NotificationManager.getSharedInstance() gets NotificationManager instance-->
[[Special:MyLanguage/NotificationManager:cancelAllNotifications|NotificationManager:cancelAllNotifications]] ''<translate>cancel scheduled notification</translate>''<br/><!-- GIDEROSMTD:NotificationManager:cancelAllNotifications() cancel scheduled notification -->
+
[[NotificationManager:cancelAllNotifications]] ''cancels scheduled notification''<br/><!--GIDEROSMTD:NotificationManager:cancelAllNotifications() cancels scheduled notification-->
[[Special:MyLanguage/NotificationManager:cancelNotification|NotificationManager:cancelNotification]] ''<translate>cancel specified notification</translate>''<br/><!-- GIDEROSMTD:NotificationManager:cancelNotification() cancel specified notification -->
+
[[NotificationManager:cancelNotification]] ''cancels specified notification''<br/><!--GIDEROSMTD:NotificationManager:cancelNotification() cancels specified notification-->
[[Special:MyLanguage/NotificationManager:clearLocalNotifications|NotificationManager:clearLocalNotifications]] ''<translate>clear local notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:clearLocalNotifications() clear local notifications -->
+
[[NotificationManager:clearLocalNotifications]] ''clears local notifications''<br/><!--GIDEROSMTD:NotificationManager:clearLocalNotifications() clears local notifications-->
[[Special:MyLanguage/NotificationManager:clearPushNotifications|NotificationManager:clearPushNotifications]] ''<translate>clear push notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:clearPushNotifications() clear push notifications -->
+
[[NotificationManager:clearPushNotifications]] ''clears push notifications''<br/><!--GIDEROSMTD:NotificationManager:clearPushNotifications() clears push notifications-->
[[Special:MyLanguage/NotificationManager:getLocalNotifications|NotificationManager:getLocalNotifications]] ''<translate>get local notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:getLocalNotifications() get local notifications -->
+
[[NotificationManager:getLocalNotifications]] ''gets local notifications''<br/><!--GIDEROSMTD:NotificationManager:getLocalNotifications() gets local notifications-->
[[Special:MyLanguage/NotificationManager:getPushNotifications|NotificationManager:getPushNotifications]] ''<translate>get push notification</translate>''<br/><!-- GIDEROSMTD:NotificationManager:getPushNotifications() get push notification -->
+
[[NotificationManager:getPushNotifications]] ''gets push notification''<br/><!--GIDEROSMTD:NotificationManager:getPushNotifications() gets push notification-->
[[Special:MyLanguage/NotificationManager:getScheduledNotifications|NotificationManager:getScheduledNotifications]] ''<translate>get schedule notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:getScheduledNotifications() get schedule notifications -->
+
[[NotificationManager:getScheduledNotifications]] ''gets schedule notifications''<br/><!--GIDEROSMTD:NotificationManager:getScheduledNotifications() gets schedule notifications-->
[[Special:MyLanguage/NotificationManager:registerForPushNotifications|NotificationManager:registerForPushNotifications]] ''<translate>register for push notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:registerForPushNotifications() register for push notifications -->
+
[[NotificationManager:registerForPushNotifications]] ''registers for push notifications''<br/><!--GIDEROSMTD:NotificationManager:registerForPushNotifications() registers for push notifications-->
[[Special:MyLanguage/NotificationManager:unregisterForPushNotifications|NotificationManager:unregisterForPushNotifications]] ''<translate>unregister from notifications</translate>''<br/><!-- GIDEROSMTD:NotificationManager:unregisterForPushNotifications() unregister from notifications -->
+
[[NotificationManager:unregisterForPushNotifications]] ''unregisters from notifications''<br/><!--GIDEROSMTD:NotificationManager:unregisterForPushNotifications() unregisters from notifications-->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
[[Special:MyLanguage/Event.LOCAL_NOTIFICATION|Event.LOCAL_NOTIFICATION]]<br/><!-- GIDEROSEVT:Event.LOCAL_NOTIFICATION localNotification-->
+
[[Event.LOCAL_NOTIFICATION]]<br/><!--GIDEROSEVT:Event.LOCAL_NOTIFICATION localNotification-->
[[Special:MyLanguage/Event.PUSH_NOTIFICATION|Event.PUSH_NOTIFICATION]]<br/><!-- GIDEROSEVT:Event.PUSH_NOTIFICATION pushNotification-->
+
[[Event.PUSH_NOTIFICATION]]<br/><!--GIDEROSEVT:Event.PUSH_NOTIFICATION pushNotification-->
[[Special:MyLanguage/Event.PUSH_REGISTRATION|Event.PUSH_REGISTRATION]]<br/><!-- GIDEROSEVT:Event.PUSH_REGISTRATION pushRegistration-->
+
[[Event.PUSH_REGISTRATION]]<br/><!--GIDEROSEVT:Event.PUSH_REGISTRATION pushRegistration-->
[[Special:MyLanguage/Event.PUSH_REGISTRATION_ERROR|Event.PUSH_REGISTRATION_ERROR]]<br/><!-- GIDEROSEVT:Event.PUSH_REGISTRATION_ERROR pushRegistrationError-->
+
[[Event.PUSH_REGISTRATION_ERROR]]<br/><!--GIDEROSEVT:Event.PUSH_REGISTRATION_ERROR pushRegistrationError-->
=== <translate>Constants</translate> ===
+
 
 +
=== Constants ===
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 00:30, 16 November 2025

Supported platforms:
Available since: Gideros 2011.6
Inherits from: EventDispatcher

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")

Methods

NotificationManager.getSharedInstance gets NotificationManager instance
NotificationManager:cancelAllNotifications cancels scheduled notification
NotificationManager:cancelNotification cancels specified notification
NotificationManager:clearLocalNotifications clears local notifications
NotificationManager:clearPushNotifications clears push notifications
NotificationManager:getLocalNotifications gets local notifications
NotificationManager:getPushNotifications gets push notification
NotificationManager:getScheduledNotifications gets schedule notifications
NotificationManager:registerForPushNotifications registers for push notifications
NotificationManager:unregisterForPushNotifications unregisters from notifications

Events

Event.LOCAL_NOTIFICATION
Event.PUSH_NOTIFICATION
Event.PUSH_REGISTRATION
Event.PUSH_REGISTRATION_ERROR

Constants