|
| 1 | +import { LocalNotifications, LocalNotificationSchema } from '@capacitor/local-notifications'; |
| 2 | +import { |
| 3 | + IonButton, |
| 4 | + IonInput, |
| 5 | + IonItem, |
| 6 | + IonLabel, |
| 7 | + IonList, |
| 8 | + IonListHeader, |
| 9 | + IonSelect, |
| 10 | + IonSelectOption, |
| 11 | +} from '@ionic/react'; |
| 12 | +import { isPlatform } from '@ionic/react'; |
| 13 | +import React, { useState } from 'react'; |
| 14 | + |
| 15 | +import styles from './LocalNotificationsOptionsTest.module.css'; |
| 16 | + |
| 17 | +export default function LocalNotificationsOptionsTest() { |
| 18 | + const [relevanceScore, setRelevanceScore] = useState<number>(0.5); |
| 19 | + const [interruptionLevel, setInterruptionLevel] = useState< |
| 20 | + 'active' | 'critical' | 'passive' | 'timeSensitive' |
| 21 | + >('active'); |
| 22 | + |
| 23 | + const notSupported = !isPlatform('ios'); |
| 24 | + |
| 25 | + const generateId = (): number => Math.floor(Math.random() * 10); |
| 26 | + |
| 27 | + const createNotification = (): LocalNotificationSchema => { |
| 28 | + return { |
| 29 | + id: generateId(), |
| 30 | + title: 'Get 10% off!', |
| 31 | + body: 'Swipe now to learn more', |
| 32 | + sound: 'beep.aiff', |
| 33 | + attachments: [{ id: 'face', url: 'res:///assets/ionitron.png' }], |
| 34 | + actionTypeId: 'OPEN_PRODUCT', |
| 35 | + extra: { |
| 36 | + productId: 'PRODUCT-1', |
| 37 | + }, |
| 38 | + }; |
| 39 | + }; |
| 40 | + |
| 41 | + const scheduleWithOptions = async () => { |
| 42 | + const notifications: LocalNotificationSchema[] = [ |
| 43 | + { |
| 44 | + ...createNotification(), |
| 45 | + relevanceScore, |
| 46 | + interruptionLevel, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + const result = await LocalNotifications.schedule({ notifications }); |
| 50 | + console.log('schedule result:', result); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className={styles.container}> |
| 55 | + {notSupported && ( |
| 56 | + <div className={styles.notSupported}> |
| 57 | + <h2>Not supported on Android</h2> |
| 58 | + </div> |
| 59 | + )} |
| 60 | + <IonList> |
| 61 | + <IonListHeader>Schedule with options (iOS 15+)</IonListHeader> |
| 62 | + <IonItem> |
| 63 | + <IonLabel position="stacked">Relevance Score (0.0 – 1.0)</IonLabel> |
| 64 | + <IonInput |
| 65 | + type="number" |
| 66 | + min="0" |
| 67 | + max="1" |
| 68 | + step="0.1" |
| 69 | + value={relevanceScore} |
| 70 | + onIonChange={e => setRelevanceScore(parseFloat(e.detail.value ?? '0.5'))} |
| 71 | + /> |
| 72 | + </IonItem> |
| 73 | + <IonItem> |
| 74 | + <IonLabel>Interruption Level</IonLabel> |
| 75 | + <IonSelect |
| 76 | + value={interruptionLevel} |
| 77 | + onIonChange={e => setInterruptionLevel(e.detail.value)} |
| 78 | + > |
| 79 | + <IonSelectOption value="active">active</IonSelectOption> |
| 80 | + <IonSelectOption value="critical">critical</IonSelectOption> |
| 81 | + <IonSelectOption value="passive">passive</IonSelectOption> |
| 82 | + <IonSelectOption value="timeSensitive">timeSensitive</IonSelectOption> |
| 83 | + </IonSelect> |
| 84 | + </IonItem> |
| 85 | + </IonList> |
| 86 | + <IonButton expand="block" onClick={scheduleWithOptions}> |
| 87 | + Schedule with options |
| 88 | + </IonButton> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments