This namespace represents the Cocoon Notification extension for local and remote notifications.
Example
//Set up notification listener Cocoon.Notification.Local.on("notification", function(userData){ console.log("A local notification has been received: " + JSON.stringify(userData)); }); Cocoon.Notification.Local.initialize(); //ready to start receiving notification callbacks // Initialize the service. Ready to start receiving notification callbacks // The service will be automatically registered after the initialization. This may show a Dialog to request user permissions. // You can disable autoregister with {register: false} params and call Cocoon.Notification.Local.register() manually to control when the permissions dialog is shown. // By default the register parameter is set to true. Cocoon.Notification.Local.initialize({}, function(registered) { if (!registered) { alert('Notifications disabled by user') } else { sendNotification(); } }) function sendNotification() { var notification = { message : "Hi, I am a notification", soundEnabled : true, badgeNumber : 0, userData : {"key1" : "value1", "key2": "value2"}, contentBody : "", contentTitle : "", date : new Date().valueOf() + 1000 icon : "myicon" }; Cocoon.Notification.Local.send(notification); }
Namespaces
Methods
-
<static> cancel(The)
-
Cancels the local notification with Id provided
Parameters:
Name Type Description The
string notification id
-
<static> cancelAllNotifications()
-
Cancels all the pending notifications
-
<static> canReceiveNotifications(callback)
-
Checks if user has permission to receive notifications
Parameters:
Name Type Description callback
function The callback function. It receives the following parameters:
- granted {Boolean} True if the user has permission to receive notifications
-
<static> fetchSubscribedChannels(callback)
-
Asynchronously get all the channels that this device is subscribed to. Valid for notification services that support specific channels (e.g. Parse).
Parameters:
Name Type Description callback
function Callback function to get the channel list. It receives the following parameters:
- channels {array} the channels that this device is subscribed to
- error.
-
<static> getBadgeNumber(callback)
-
(iOS only) Gets the current badge number.
Parameters:
Name Type Description callback
function The callback to get badgeNumber. It receives the following parameters:
- badgeNumber.
-
<static> initialize(callback)
-
Starts the Notification Service. The notification callbacks will start to be received after calling this method. Because of this, you should have set your event handler before calling this method, so you won't lose any callback. The service will be automatically registered after the initialization. This may show a Dialog to request user permissions. You can disable autoregister with {register: false} params and call Cocoon.Notification.Local.register() manually to control when the permissions dialog is shown. By default the register parameter is set to true.
Parameters:
Name Type Description params.
Object Service dependant params
callback
function The callback function. It receives the following parameters:
- Registered: True if the devices is already registered to receive notifications
- Error.
-
<static> register(params, callback)
-
Registers the application to be able to receive push notifications. In some systems like iOS it shows a dialog to request user permission.
Parameters:
Name Type Description params
Object callback
function The callback function. It receives the following parameters:
- Error.
-
<static> send(notification, callback)
-
Send notification
Parameters:
Name Type Description notification
Cocoon.Notification.NotificationInfo The notification to sends
callback
function The delivery callback function. It receives the following parameters:
- Error.
-
<static> setBadgeNumber(badgeNumber)
-
(iOS only) Sets the badge number for this application.
Parameters:
Name Type Description badgeNumber
number The badge number
-
<static> subscribe(channel, callback)
-
Subscribes to a channel in order to receive notifications targeted to that channel. Valid for notification services that support specific channels (e.g. Parse).
Parameters:
Name Type Description channel
string The channel id
callback
function The subscribe succeeded callback function. It receives the following parameters:
- Error.
-
<static> unregister(params, callback)
-
Unregisters the application from receiving push notifications
Parameters:
Name Type Description params
Object callback
function The callback function. It receives the following parameters:
- Error.
-
<static> unsubscribe(channel, callback)
-
Unsubscribes from a channel in order to stop receiving notifications targeted to it. Valid for notification services that support specific channels (e.g. Parse).
Parameters:
Name Type Description channel
string The channel id
callback
function The unsubscribe succeeded callback function. It receives the following parameters:
- Error.
Events
-
On notification
-
Allows to listen to events when the application receives a notification.
Example
Cocoon.Notification.Local.on("notification", function(data) { console.log('Local notification received: ' + JSON.stringify(data)); }); Cocoon.Notification.Parse.on("notification", function(data) { console.log('Remote notification received: ' + JSON.stringify(data)); });
Members
-
<static> NotificationInfo
-
The object that represents the information of a notification.
Properties:
Name Type Description Cocoon.Notification.NotificationInfo
object The object itself
Properties
Name Type Description id
string The notification identifier. Autogenerated.
title
string The notification message. By default, it will be empty.
message
string The notification title. By default, it will be empty.
soundEnabled
boolean A flag that indicates if the sound should be enabled for the notification. By default, it will be true.
badgeNumber
number The number that will appear in the badge of the application icon in the home screen. By default, it will be 0.
userData
object The JSON data to attached to the notification. By default, it will be empty.
contentBody
string The body content to be showed in the expanded notification information. By default, it will be empty.
contentTitle
string The title to be showed in the expanded notification information. By default, it will be empty.
date
number Time in millisecs from 1970 when the notification will be fired. By default, it will be 1 second (1000).
icon
string (Optional) (Android only) Name of the custom icon that you have bundled in the application as a resource (You will need a custom Cordova plugin to bundle resources in the app). In Android is the icon name without extension.