Files
aria 61e04723f3 updated JavaScript utils file to include new methods.
created a unified flow to be an example.
2026-05-21 13:27:31 +09:00

119 lines
3.9 KiB
JavaScript

'use strict';
(function (root) {
function getBluememeUtils() {
var cordovaPlugin = root.cordova
&& root.cordova.plugins
&& root.cordova.plugins.bluememeUtils;
var plugin = root.BluememeUtils || cordovaPlugin;
if (!plugin) {
throw new Error('BluememeUtils is not available. Run this flow after deviceready.');
}
return plugin;
}
function toPromise(work) {
return new Promise(function (resolve, reject) {
work(resolve, reject);
});
}
function call(methodName) {
var args = Array.prototype.slice.call(arguments, 1);
return toPromise(function (resolve, reject) {
var plugin = getBluememeUtils();
plugin[methodName].apply(plugin, args.concat(resolve, reject));
});
}
function isPushPermissionGranted(permission) {
return !!permission && (permission.enabled === true || permission.granted === true);
}
function permissionRequiredError(permission) {
return {
code: 'PERMISSION_REQUIRED',
message: 'Notification permission is required before Firebase messaging setup.',
permission: permission || null
};
}
function ensurePushPermission() {
return call('getPushPermissionStatus').then(function (status) {
if (isPushPermissionGranted(status)) {
return status;
}
return call('requestPushPermission').then(function () {
return call('getPushPermissionStatus');
}).then(function (updatedStatus) {
if (isPushPermissionGranted(updatedStatus)) {
return updatedStatus;
}
throw permissionRequiredError(updatedStatus);
});
});
}
function subscribeTopics(topics) {
return (topics || []).reduce(function (chain, topic) {
return chain.then(function (results) {
return call('subscribeToTopic', topic).then(function () {
results.push(topic);
return results;
});
});
}, Promise.resolve([]));
}
function startListener(onNotificationEvent, onError) {
if (typeof onNotificationEvent !== 'function') {
return;
}
getBluememeUtils().startNotificationListener(onNotificationEvent, onError);
}
function setupFirebaseMessaging(options, success, error) {
options = options || {};
var flow = ensurePushPermission()
.then(function (permission) {
startListener(options.onNotificationEvent, error || options.onError);
return call('getFirebaseToken').then(function (tokenResult) {
return {
permission: permission,
token: tokenResult && tokenResult.token,
tokenResult: tokenResult
};
});
})
.then(function (context) {
return subscribeTopics(options.topics).then(function (topics) {
context.topics = topics;
return context;
});
})
.then(function (context) {
return call('getInitialNotification').then(function (initialNotification) {
context.initialNotification = initialNotification || null;
return context;
});
});
if (typeof success === 'function' || typeof error === 'function') {
flow.then(success, error);
}
return flow;
}
root.BluememePushFlow = {
ensurePushPermission: ensurePushPermission,
isPushPermissionGranted: isPushPermissionGranted,
setupFirebaseMessaging: setupFirebaseMessaging
};
})(window);