CordovaShims.Services.Notifications Class
Service that allows for registration/unregistration and handling of push notifications received from GCM or APNs.
Configuration and setup of the service is done in an initializer:
import NotificationsService from 'cordova-shims/services/notifications';
export function initialize(container, application) {
let service = NotificationsService.create({
gcmSenderId: '{INSERT-KEY-FROM-GCM-HERE}',
gcmTimeout: 10000 // Timeout for GCM in ms. Default: 15000
});
application.register('service:notifications', service, {
instantiate: false
});
application.inject('route', 'notifications', 'service:notifications');
}
export default {
name: 'notifications-service',
initialize: initialize
};
Item Index
Properties
Events
Methods
_playSound
-
filename
Plays a sound on the device. Fails silently.
Parameters:
-
filename
StringFilename for the sound to play.
_setBadgeCount
-
badgeCount
Sets the badge count on the app icon if possible. Fails silently.
Parameters:
-
badgeCount
NumberThe current badge count.
_subscribeForEcb
()
private
Sets up callbacks expected by PushPlugin
register
()
RSVP.Promise
Registers the device for push notifications.
Example:
// app/routes/application.js
import Em from 'ember';
export default Em.Route.extend({
actions: {
subscribeForNotifications: function(){
let store = this.store;
this.notifications.register().then(function(data){
let network = data.network,
token = data.token;
return store.createRecord('device', {
network: network,
token: token
}).save();
}).then(function(deviceModelFromStore){
Em.Logger.info('Successfully registered for notifications');
}).catch(function(error){
Em.Logger.error('Something went wrong registering for notifications', error);
});
}
}
});
Returns:
A promise that will resolve with an object containing
the network
(apns or gcm) and the token
for the
device.
unregister
()
RSVP.Promise
Unregisters this specific device for push notifications.
Example:
// app/routes/application.js
import Em from 'ember';
export default Em.Route.extend({
actions: {
unsubscribeNotifications: function(){
this.notifications.unregister().then(function(){
Em.Logger.info('Successfully unregistered the device');
}).catch(function(error){
Em.Logger.error('Something went wrong while unregistering', error);
});
}
}
});
Returns:
A promise that will resolve when registration is successful.
Properties
gcmSenderId
String
Project ID from Google API Console. Must be set if you want to register devices with GCM.
gcmTimeout
Number
Timeout for GCM registration. In ms.
Default: 15000
Events
alert
Event Payload:
-
message
StringThe message received in the payload
badge
Event Payload:
-
count
NumberBadge count recieved in the payload
registered
private
Please do not attach event listeners to this event. Use the register
method
instead to handle device registration.
Event Payload:
-
data
ObjectAn object with a
network
and atoken
.network
will be either 'apns' or 'gcm'.
sound
Event Payload:
-
filename
StringFilename of the sound that should be played