/* AeroGear JavaScript Library
* https://github.com/aerogear/aerogear-js
* JBoss, Home of Professional Open Source
* Copyright Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function( AeroGear, $, undefined ) {
/**
The SimplePushClient object is used as a sort of polyfill/implementation of the SimplePush spec implemented in Firefox OS and the Firefox browser and provides a mechanism for subscribing to and acting on push notifications in a web application. See https://wiki.mozilla.org/WebAPI/SimplePush
@status Experimental
@constructs AeroGear.SimplePushClient
@param {Object} options - an object used to initialize the connection to the SimplePush server
@param {Boolean} [options.useNative=false] - if true, the connection will first try to use the Mozilla push network (still in development and not ready for production) before falling back to the SimplePush server specified
@param {String} [options.simplePushServerURL] - the URL of the SimplePush server. This option is optional but only if you don't want to support browsers that are missing websocket support and you trust the not yet production ready Mozilla push server.
@param {Function} options.onConnect - a callback to fire when a connection is established with the SimplePush server. This is a deviation from the SimplePush spec as it is not necessary when you using the in browser functionality since the browser establishes the connection before the application is started.
@param {Function} options.onClose - a callback to fire when a connection to the SimplePush server is closed or lost.
@returns {Object} The created unified push server client
@example
// Create the SimplePushClient object:
var client = AeroGear.SimplePushClient({
simplePushServerURL: "https://localhost:7777/simplepush",
onConnect: myConnectCallback,
onClose: myCloseCallback
});
*/
AeroGear.SimplePushClient = function( options ) {
// Allow instantiation without using new
if ( !( this instanceof AeroGear.SimplePushClient ) ) {
return new AeroGear.SimplePushClient( options );
}
this.options = options || {};
// Check for native push support
if ( !!navigator.push && this.options.useNative ) {
// Browser supports push so let it handle it
if ( options.onConnect ) {
options.onConnect();
}
return;
}
if ( this.options.useNative ) {
if ("WebSocket" in window ) {
// No native push support but want to use Mozilla servers
this.options.simplePushServerURL = "wss://push.services.mozilla.com";
} else if ( !this.options.simplePushServerURL ) {
// No native push support and no websocket support so can't talk to Mozilla server
throw "SimplePushConfigurationError";
} else {
// No websocket, no native support but SimplePush server specified so try SockJS connection
this.options.useNative = false;
}
}
var spClient = this,
connectOptions = {
onConnect: function() {
/**
The window.navigator object
@namespace navigator
*/
/**
Add the push object to the global navigator object
@status Experimental
@constructs navigator.push
*/
navigator.push = (function() {
return {
/**
Register a push notification channel with the SimplePush server
@function
@memberof navigator.push
@returns {Object} - The request object where a connection success callback can be registered
@example
var mailRequest = navigator.push.register();
*/
register: function() {
var request = {
onsuccess: function( event ) {}
};
if ( !spClient.simpleNotifier ) {
throw "SimplePushConnectionError";
}
spClient.simpleNotifier.subscribe({
requestObject: request
});
return request;
},
/**
Unregister a push notification channel from the SimplePush server
@function
@memberof navigator.push
@example
navigator.push.unregister( mailEndpoint );
*/
unregister: function( endpoint ) {
spClient.simpleNotifier.unsubscribe( endpoint );
},
/**
Reestablish the connection with the SimplePush server when closed or lost. This is an addition and not part of the SimplePush spec
@function
@memberof navigator.push
@param {Object} options - an object used to initialize the connection to the SimplePush server
@param {String} options.simplePushServerURL - the URL of the SimplePush server
@param {Function} options.onConnect - a callback to fire when a connection is established with the SimplePush server. This is a deviation from the SimplePush spec as it is not necessary when you using the in browser functionality since the browser establishes the connection before the application is started.
@param {Function} options.onClose - a callback to fire when a connection to the SimplePush server is closed or lost.
@example
navigator.push.reconnect({
simplePushServerURL: "https://localhost:7777/simplepush",
onConnect: myConnectCallback,
onClose: myCloseCallback
});
*/
reconnect: function() {
spClient.simpleNotifier.connect( connectOptions );
}
};
})();
/**
Add the setMessageHandler/mozSetMessageHandler function to the global navigator object
@status Experimental
@constructs navigator.setMessageHandler/navigator.mozSetMessageHandler
@param {String} messageType - a name or category to give the messages being received and in this implementation, likely 'push'
@param {Function} callback - the function to be called when a message of this type is received
@example
navigator.setMessageHandler( "push", function( message ) {
if ( message.channelID === mailEndpoint.channelID ) {
console.log("Mail Message Received");
}
});
or
// Mozilla's spec currently has the 'moz' prefix
navigator.mozSetMessageHandler( "push", function( message ) {
if ( message.channelID === mailEndpoint.channelID ) {
console.log("Mail Message Received");
}
});
*/
navigator.setMessageHandler = navigator.mozSetMessageHandler = function( messageType, callback ) {
$( navigator.push ).on( messageType, function( event ) {
var message = event.message;
callback.call( this, message );
});
};
if ( spClient.options.onConnect ) {
spClient.options.onConnect();
}
},
onClose: function() {
spClient.simpleNotifier.disconnect( spClient.options.onClose );
}
};
// Create a Notifier connection to the Push Network
spClient.simpleNotifier = AeroGear.Notifier({
name: "agPushNetwork",
type: "SimplePush",
settings: {
connectURL: spClient.options.simplePushServerURL,
useNative: spClient.options.useNative
}
}).clients.agPushNetwork;
spClient.simpleNotifier.connect( connectOptions );
};
})( AeroGear, jQuery );