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 Thestring notification id
-
<static> cancelAllNotifications()
-
Cancels all the pending notifications
-
<static> canReceiveNotifications(callback)
-
Checks if user has permission to receive notifications
Parameters:
Name Type Description callbackfunction 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 callbackfunction 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 callbackfunction 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
callbackfunction 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 paramsObject callbackfunction The callback function. It receives the following parameters:
- Error.
-
<static> send(notification, callback)
-
Send notification
Parameters:
Name Type Description notificationCocoon.Notification.NotificationInfo The notification to sends
callbackfunction 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 badgeNumbernumber 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 channelstring The channel id
callbackfunction 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 paramsObject callbackfunction 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 channelstring The channel id
callbackfunction 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.NotificationInfoobject The object itself
Properties
Name Type Description idstring The notification identifier. Autogenerated.
titlestring The notification message. By default, it will be empty.
messagestring The notification title. By default, it will be empty.
soundEnabledboolean A flag that indicates if the sound should be enabled for the notification. By default, it will be true.
badgeNumbernumber The number that will appear in the badge of the application icon in the home screen. By default, it will be 0.
userDataobject The JSON data to attached to the notification. By default, it will be empty.
contentBodystring The body content to be showed in the expanded notification information. By default, it will be empty.
contentTitlestring The title to be showed in the expanded notification information. By default, it will be empty.
datenumber Time in millisecs from 1970 when the notification will be fired. By default, it will be 1 second (1000).
iconstring (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.