pwanat-sdk
v1.0.6
Published
A cordova native helper for front-end applications
Downloads
15
Maintainers
Readme
Progressive Web App Native Support
A cordova native helper for front-end applicationss
This package provides a set of utilities for interacting with webviews, notifications, file management, Firebase messaging, hot code updates and more in a Cordova/PhoneGap environment.
Pwanat
The Pwanat
object provides various utilities for detecting the platform, managing device settings, performing clipboard actions and many more. Below are the available methods and their descriptions.
Properties:
- isNative: A boolean indicating if the app is running in a native environment.
- platform: A string that stores the platform information.
Methods:
init(): Initializes the Pwanat object, typically resolving after detecting the platform or native state.
await Pwanat.init();
detectDevice(): Detects the current device and returns device information.
const device = Pwanat.detectDevice();
getDeviceInformation(): Fetches and returns information about the device.
const info = Pwanat.getDeviceInformation();
getBuildInfo(): Returns information about the build version.
const buildInfo = Pwanat.getBuildInfo();
showAlert(message): Displays a native alert with the specified message.
Pwanat.showAlert('This is an alert message');
lockOrientation(orientation = "portrait-primary"): Locks the screen orientation.
Pwanat.lockOrientation('landscape');
copy(text): Copies the specified text to the clipboard.
Pwanat.copy('Copied text');
paste(): Asynchronously retrieves the current content from the clipboard.
const clipboardContent = await Pwanat.paste();
setAndroidFullScreen(): Sets the app to full-screen mode on Android devices.
Pwanat.setAndroidFullScreen();
resetAndroidFullScreen(): Resets full-screen mode on Android devices.
Pwanat.resetAndroidFullScreen();
Features
Browser:
system(url)
: Opens a system browser with the provided URL.webview(url, options)
: Opens a webview with the given URL and options.closeWebview()
: Closes the currently open webview.
File:
saveImageFromUrl(url, filename)
: Downloads an image from a URL and saves it to the local file system.saveImageFromBase64(base64Image, filename)
: Saves a base64-encoded image to the local file system.
Notification:
showLocalNotification(title, text)
: Displays a local notification with the given title and text.setBadge(count)
: Sets the app icon badge number.firebaseGetToken()
: Retrieves the Firebase device token asynchronously.registerFirebaseListener()
: Registers Firebase event listeners for token refresh and incoming messages.
Social Share:
appShare(msg, url)
: Shares a message via the app.fb(msg, url)
: Shares a message to Facebook.ig(msg, url)
: Shares a message to Instagram.whatsapp(msg, url)
: Shares a message via WhatsApp.tg(msg, url)
: Shares a message via Telegram.
HCP (Hot Code Push):
checkUpdates(versionJsonUrl, currentVersion)
: Checks for app updates based on the given version file URL.downloadPackage(packageUrl, version)
: Downloads a package from the provided URL and version.installPackage(fileURL)
: Installs the package from the given file URL.deleteZipFile(fileURL)
: Deletes a zip file from the given file URL.
Google:
referrer()
: Fetches the Google Play referrer information.
Installation
To install this package, run:
npm install pwanat-sdk
Usage
Provide usage examples for the main functionality of your package.
Example 1: Opening a Webview
import Pwanat from 'pwanat-sdk';
const result = await Pwanat.Browser.webview('https://example.com', {
title: 'Example',
fullscreen: true
});
if (result.status === 0) {
console.log('Failed to create a new webview', result.msg);
}
Example 2: Saving an Image from Base64
import Pwanat from 'pwanat-sdk';
const result = await Pwanat.File.saveImageFromBase64(base64String, 'myImage.png');
if (result.status === 1) {
console.log('Image saved successfully!', result);
} else {
console.log('Error saving!');
}
Example 3: Registering Firebase Listeners
import Pwanat from 'pwanat-sdk';
Pwanat.Notification.registerFirebaseListener();
Example 4: Implementing Hot Code Push
import Pwanat from 'pwanat-sdk';
const hcpUrl = 'https://example.com/version.json';
const version = import.meta.env.VITE_APP_VERSION as string
try {
const checkUpdate = await Pwanat.HCP.checkUpdates(hcpUrl, version);
if (checkUpdate.status === 1) {
switch(checkUpdate.data.type) {
case "ContentUpdate":
const handleProgress = (event: CustomEvent): void => {
checkingUpdate.value = true; // show the page for updating UI
if (event.detail.updateTitle) {
checkingUpdateTitle.value = event.detail.updateTitle;
}
if (event.detail.updateSubtitle) {
checkingUpdateSubTitle.value = event.detail.updateSubtitle;
}
if (event.detail.percent) {
checkingUpdateProgress.value = event.detail.percent;
}
};
// Listen for the update event to track the progress
window.addEventListener("appUpdateInformation", handleProgress as EventListener);
const downloadPackage = await Pwanat.HCP.downloadPackage(checkUpdate.data.packageUrl, checkUpdate.data.version);
if(downloadPackage.status === 1) {
const installPackage = await Pwanat.HCP.installPackage(downloadPackage.data);
if(installPackage.status === 1) {
window.location.reload(); // reload the page to see the updated content
}else {
Pwanat.showAlert(installPackage.msg);
}
} else {
Pwanat.showAlert(downloadPackage.msg);
}
break;
case "AppUpdate":
// open the system browser where user's can download the new apk/ipa
Pwanat.Browser.system(checkUpdate.data.appDownloadUrl);
break;
default:
// means the app is updated
break;
}
} else {
Pwanat.showAlert(checkUpdate.msg);
}
} catch (error) {
let errorMessage = "An unknown error occurred while checking for updates";
// Check if the error is an instance of Error and use its message
if (error instanceof Error) {
errorMessage = error.message;
} else if (typeof error === "string") {
errorMessage = error; // If it's a string, use it directly
}
Pwanat.showAlert(errorMessage);
}
API Reference
Browser
system(url)
: Opens the system browser with the provided URL.webview(url, options)
: Opens a webview with the provided URL and options.
File
saveImageFromUrl(url, filename)
: Downloads an image from the provided URL and saves it to the specified file path.saveImageFromBase64(base64Image, fileName = 'downloadedImage.png')
: Saves a base64-encoded image as a file.
Notification
showLocalNotification(title, text)
: Displays a local notification with the specified title and text.setBadge(count)
: Sets the app badge number to the provided count.firebaseGetToken()
: Asynchronously retrieves the Firebase token for the device.registerFirebaseListener()
: Registers event listeners for Firebase messaging.
Social Share
appShare(msg, url)
: Shares the given message and URL via the app.fb(msg, url)
: Shares the given message and URL on Facebook.ig(msg, url)
: Shares the given message and URL on Instagram.whatsapp(msg, url)
: Shares the given message and URL on WhatsApp.tg(msg, url)
: Shares the given message and URL on Telegram.
HCP
checkUpdates(versionJsonUrl, currentVersion)
: Checks for app updates based on the given version JSON URL.downloadPackage(packageUrl, version)
: Downloads a package from the provided URL and version.installPackage(fileURL)
: Installs the downloaded package from the provided file URL.deleteZipFile(fileURL)
: Deletes the zip file at the provided file URL.
referrer()
: Fetches the Google Play referrer and passes it to the callback function.
License
This project is licensed under the MIT License - see the LICENSE file for details.