-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdafc83
commit 00eeb01
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,106 @@ | ||
'use strict'; | ||
|
||
|
||
var execAsPromise = function (command, args) { | ||
if (args === void 0) { args = []; } | ||
return new Promise(function (resolve, reject) { | ||
window.cordova.exec(resolve, reject, 'FCMPlugin', command, args); | ||
}); | ||
}; | ||
|
||
var asDisposableListener = function (eventTarget, eventName, callback, options) { | ||
if (options === void 0) { options = {}; } | ||
var once = options.once; | ||
var handler = function (event) { return callback(event.detail); }; | ||
eventTarget.addEventListener(eventName, handler, { passive: true, once: once }); | ||
return { | ||
dispose: function () { return eventTarget.removeEventListener(eventName, handler); }, | ||
}; | ||
}; | ||
|
||
var bridgeNativeEvents = function (eventTarget) { | ||
var onError = function (error) { return console.log('FCM: Error listening to native events', error); }; | ||
var onEvent = function (data) { | ||
try { | ||
var _a = JSON.parse(data), eventName = _a[0], eventData = _a[1]; | ||
eventTarget.dispatchEvent(new CustomEvent(eventName, { detail: eventData })); | ||
} | ||
catch (error) { | ||
console.log('FCM: Error parsing native event data', error); | ||
} | ||
}; | ||
window.cordova.exec(onEvent, onError, 'FCMPlugin', 'startJsEventBridge', []); | ||
}; | ||
|
||
var FCMPlugin = (function () { | ||
function FCMPlugin() { | ||
var _this = this; | ||
this.eventTarget = document.createElement('div'); | ||
execAsPromise('ready') | ||
.catch(function (error) { return console.log('FCM: Ready error: ', error); }) | ||
.then(function () { | ||
console.log('FCM: Ready!'); | ||
bridgeNativeEvents(_this.eventTarget); | ||
}); | ||
console.log('FCM: has been created'); | ||
} | ||
FCMPlugin.prototype.clearAllNotifications = function () { | ||
return execAsPromise('clearAllNotifications'); | ||
}; | ||
FCMPlugin.prototype.createNotificationChannel = function (channelConfig) { | ||
if (window.cordova.platformId !== 'android') { | ||
return Promise.resolve(); | ||
} | ||
return execAsPromise('createNotificationChannel', [channelConfig]); | ||
}; | ||
FCMPlugin.prototype.deleteInstanceId = function () { | ||
return execAsPromise('deleteInstanceId'); | ||
}; | ||
FCMPlugin.prototype.getAPNSToken = function () { | ||
return window.cordova.platformId !== 'ios' | ||
? Promise.resolve('') | ||
: execAsPromise('getAPNSToken'); | ||
}; | ||
FCMPlugin.prototype.getInitialPushPayload = function () { | ||
return execAsPromise('getInitialPushPayload'); | ||
}; | ||
FCMPlugin.prototype.getToken = function () { | ||
return execAsPromise('getToken'); | ||
}; | ||
FCMPlugin.prototype.hasPermission = function () { | ||
return window.cordova.platformId !== 'ios' | ||
? Promise.resolve(true) | ||
: execAsPromise('hasPermission'); | ||
}; | ||
FCMPlugin.prototype.onNotification = function (callback, options) { | ||
return asDisposableListener(this.eventTarget, 'notification', callback, options); | ||
}; | ||
FCMPlugin.prototype.onTokenRefresh = function (callback, options) { | ||
return asDisposableListener(this.eventTarget, 'tokenRefresh', callback, options); | ||
}; | ||
FCMPlugin.prototype.requestPushPermission = function (options) { | ||
if (window.cordova.platformId !== 'ios') { | ||
return Promise.resolve(true); | ||
} | ||
var ios9SupportTimeout = 10; | ||
if (options && options.ios9Support) { | ||
ios9SupportTimeout = options.ios9Support.timeout; | ||
} | ||
var ios9SupportInterval = 0.3; | ||
if (options && options.ios9Support) { | ||
ios9SupportInterval = options.ios9Support.interval; | ||
} | ||
return execAsPromise('requestPushPermission', [ios9SupportTimeout, ios9SupportInterval]); | ||
}; | ||
FCMPlugin.prototype.subscribeToTopic = function (topic) { | ||
return execAsPromise('subscribeToTopic', [topic]); | ||
}; | ||
FCMPlugin.prototype.unsubscribeFromTopic = function (topic) { | ||
return execAsPromise('unsubscribeFromTopic', [topic]); | ||
}; | ||
return FCMPlugin; | ||
}()); | ||
|
||
var FCM = new FCMPlugin(); | ||
|
||
module.exports = FCM; |