Using Capacitor Communication Plugins
1. Using Share Plugin
| Property | Description | Values |
|---|---|---|
| share | Share data with other apps | ShareOptions |
Example: Sharing Text
import { Share } from '@capacitor/share';
const share = async () => {
await Share.share({
title: 'Check this out!',
text: 'This is some text to share',
});
};
Example: Sharing URL
import { Share } from '@capacitor/share';
const shareUrl = async () => {
await Share.share({
title: 'Check this out!',
url: 'https://example.com',
});
};
Note: The Share plugin requires appropriate permissions on native platforms.
2. Using Browser Plugin
| Property | Description | Usage |
|---|---|---|
| open | Opens a URL in the device's default browser or in-app browser | Browser.open({ url: 'https://example.com' }) |
| close | Closes the in-app browser window | Browser.close() |
| subscribeToEvent | Subscribes to browser events (e.g., 'browserOpened', 'browserClosed') | Browser.subscribeToEvent('browserOpened', (event) => { ... }) |
Example: Opening a URL in Browser
import { Browser } from '@capacitor/browser';
const openBrowser = async () => {
await Browser.open({ url: 'https://ionic.io' });
};
openBrowser();
Example: Closing Browser Window
import { Browser } from '@capacitor/browser';
const closeBrowser = async () => {
await Browser.close();
};
closeBrowser();
Note: The
Browser plugin supports both in-app and external browser modes depending on the platform and configuration.
3. Using App Launcher Plugin
| Property | Description | Usage |
|---|---|---|
| AppLauncher | Capacitor plugin for launching external apps or URLs | import { AppLauncher } from '@capacitor/app-launcher'; |
Example: Launching a URL
import { AppLauncher } from '@capacitor/app-launcher';
const launchUrl = async () => {
await AppLauncher.openUrl({
url: 'https://ionic.io',
});
};
Example: Launching a Native App
import { AppLauncher } from '@capacitor/app-launcher';
const launchNativeApp = async () => {
await AppLauncher.openUrl({
url: 'com.google.android.apps.maps',
});
};
Note: On iOS,
openUrl requires the URL scheme to be added to LSApplicationQueriesSchemes in Info.plist.
Warning: Not all platforms support launching native apps via package names. Android requires the app to be installed.
4. Using Local Notifications
| Property | Description | Values |
|---|---|---|
| id | Unique identifier for the notification | string |
| title | Title of the notification | string |
| body | Body content of the notification | string |
| schedule | Scheduling options for the notification | object |
| schedule.at | Time to trigger the notification | Date |
| schedule.repeat | Repeat interval for the notification | 'day' | 'week' | 'month' | 'year' |
| sound | Sound to play when notification is triggered | string |
| badge | Badge count to display on app icon | number |
Example: Schedule a local notification
import { LocalNotifications } from '@capacitor/local-notifications';
LocalNotifications.schedule({
notifications: [
{
id: '1',
title: 'Reminder',
body: 'Time to take a break!',
schedule: {
at: new Date(Date.now() + 5000),
},
sound: 'default',
badge: 1,
}
]
});
Example: Cancel a notification
import { LocalNotifications } from '@capacitor/local-notifications';
LocalNotifications.cancel({
notifications: [
{
id: '1'
}
]
});
Note: On Android, local notifications require the
POST_NOTIFICATIONS permission to be granted by the user.
Warning: Notifications scheduled in the past will not be shown.
5. Using Push Notifications
| Property | Description | Default |
|---|---|---|
| pushNotification | Configuration for push notifications | {} |
| senderId | Sender ID for FCM (Firebase Cloud Messaging) | null |
| requestPermissions | Whether to request notification permissions on app start | true |
Example: Register for Push Notifications
import { Plugins } from '@capacitor/core';<br><br>const { PushNotifications } = Plugins;<br><br>PushNotifications.requestPermissions().then(result => {<br> if (result.receive === 'granted') {<br> console.log('Push notification permission granted');<br> } else {<br> console.log('Push notification permission denied');<br> }<br>});
Example: Handle Push Notification Events
PushNotifications.addListener('pushNotificationReceived', (notification) => {<br> console.log('Push notification received:', notification);<br>});<br><br>PushNotifications.addListener('pushNotificationActionPerformed', (notification) => {<br> console.log('Push notification action performed:', notification);<br>});
Note: Push notifications require platform-specific setup (e.g., Firebase for Android, APNs for iOS).
Warning: On Android, ensure FCM is properly configured in your Firebase Console.
Push Notification Setup Steps
- Set up Firebase project and get FCM server key
- Configure Android/iOS push notification services
- Request permissions from the user
- Handle notification received and action performed events
6. Using Action Sheet Plugin
| Property | Description | Values |
|---|---|---|
| header | Header text for the action sheet | string |
| subHeader | Subheader text for the action sheet | string |
| buttons | Array of buttons to display | Array<ActionSheetButton> |
| cancelable | Whether the action sheet can be dismissed by tapping outside | boolean |
Example: Show Action Sheet
import { ActionSheetPlugin } from '@capacitor/action-sheet';
const showActionSheet = async () => {
const result = await ActionSheetPlugin.showActionSheet({
header: 'Choose an option',
subHeader: 'Select from the options below',
buttons: [
{
title: 'Option 1',
icon: 'add-circle-outline'
},
{
title: 'Option 2',
icon: 'checkmark-circle-outline'
},
{
title: 'Cancel',
icon: 'close-circle-outline',
role: 'cancel'
}
],
cancelable: true
});
console.log('Action Sheet result:', result);
};
Note: The
icon property uses Ionicons names. Ensure the appropriate icon set is included in your project.Key Features
- Header and Subheader support rich text formatting
- Buttons can include icons and roles (e.g.,
cancel) - Cancelable option allows dismissing by tapping outside
7. Using Dialog Plugin
| Property | Description | Values |
|---|---|---|
| alert | Displays an alert dialog with a message and optional title | Promise<void> |
| confirm | Displays a confirmation dialog with Yes/No options | Promise<boolean> |
| prompt | Displays a dialog with a message and input field | Promise<{value: string}> |
Example: Alert Dialog
import { Dialog } from '@capacitor/dialog';
await Dialog.alert({
title: 'Hello Ionic!',
message: 'This is an alert dialog.',
});
Example: Confirmation Dialog
import { Dialog } from '@capacitor/dialog';
const result = await Dialog.confirm({
title: 'Confirm Action',
message: 'Do you want to proceed?',
});
if (result.value) {
console.log('User confirmed');
} else {
console.log('User cancelled');
}
Example: Prompt Dialog
import { Dialog } from '@capacitor/dialog';
const result = await Dialog.prompt({
title: 'Enter Name',
message: 'Please enter your name:',
});
if (result.value) {
console.log('User entered:', result.value);
}
Note: The Dialog plugin is available on all platforms (iOS, Android, Web) but behavior may vary slightly.
8. Using Toast Plugin
| Property | Description | Default | Notes |
|---|---|---|---|
| text | Text to display in the toast | "" | Required |
| duration | Duration to show toast (ms) | 2000 | Can be "short" (2000ms) or "long" (3000ms) |
| position | Position of toast on screen | "bottom" | Options: "top", "center", "bottom" |
| color | Color of toast background | "primary" | Options: "primary", "secondary", "success", "warning", "danger" |
Example: Show Toast
<script lang="ts">
import { Toast } from '@capacitor/toast';
const showToast = async () => {
await Toast.show({
text: 'Hello Ionic!',
duration: 'short',
position: 'bottom',
color: 'success'
});
};
</script>
Example: Show Long Toast
<script lang="ts">
import { Toast } from '@capacitor/toast';
const showLongToast = async () => {
await Toast.show({
text: 'This is a long-running operation',
duration: 'long',
position: 'center'
});
};
</script>
9. Using Motion Plugin
| Property | Description | Values |
|---|---|---|
| motion | Access to motion sensor data | accelerometer, gyroscope, magnetometer |
Example: Get Accelerometer Data
import { Motion } from '@capacitor-community/motion';
const getMotionData = async () => {
const result = await Motion.getAccelerometer();
console.log('Acceleration X:', result.x);
console.log('Acceleration Y:', result.y);
console.log('Acceleration Z:', result.z);
};
Example: Get Gyroscope Data
import { Motion } from '@capacitor-community/motion';
const getGyroData = async () => {
const result = await Motion.getGyroscope();
console.log('Rotation X:', result.x);
console.log('Rotation Y:', result.y);
console.log('Rotation Z:', result.z);
};
Example: Get Magnetometer Data
import { Motion } from '@capacitor-community/motion';
const getMagnetometerData = async () => {
const result = await Motion.getMagnetometer();
console.log('Magnetic X:', result.x);
console.log('Magnetic Y:', result.y);
console.log('Magnetic Z:', result.z);
};
Note: Ensure that the device has motion sensors enabled and that the app has appropriate permissions to access sensor data.
10. Using Screen Reader Plugin
| Property | Description | Values |
|---|---|---|
| available | Checks if the screen reader is available on the device | true or false |
| isActive | Checks if the screen reader is currently active | true or false |
Example: Check if screen reader is available
import { ScreenReader } from '@capacitor-community/screen-reader';
ScreenReader.available().then(available => {
console.log('Screen reader available:', available);
});
Example: Check if screen reader is active
import { ScreenReader } from '@capacitor-community/screen-reader';
ScreenReader.isActive().then(active => {
console.log('Screen reader active:', active);
});
Example: Speak text using screen reader
import { ScreenReader } from '@capacitor-community/screen-reader';
ScreenReader.speak({
text: 'Hello, world!',
language: 'en-US'
});
Note: The Screen Reader plugin requires appropriate permissions on the device to function properly. Ensure that your app has the necessary permissions granted by the user.