Skip to content

Commit dac3062

Browse files
feat(local-notifications): add test component for iOS 15+ options (relevanceScore and interruptionLevel)
1 parent 915280e commit dac3062

3 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.container {
2+
position: relative;
3+
}
4+
5+
.notSupported {
6+
position: absolute;
7+
display: flex;
8+
justify-content: center;
9+
align-items: center;
10+
text-transform: uppercase;
11+
top: 0;
12+
left: 0;
13+
width: 100%;
14+
height: 100%;
15+
background-color: rgba(0, 0, 0, 0.85);
16+
z-index: 10;
17+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

src/pages/LocalNotifications.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@ionic/react';
1313
import React, { useEffect, useState } from 'react';
1414
import LocalNotificationTest from '../components/LocalNotificationTest';
15+
import LocalNotificationsOptionsTest from '../components/LocalNotificationsOptionsTest';
1516
import NotificationChannelsTest from '../components/NotificationChannelsTest';
1617

1718
import './LocalNotifications.css';
@@ -137,6 +138,8 @@ const LocalNotificationsPage: React.FC = () => {
137138
<IonContent>
138139
<LocalNotificationTest permissions={hasPermission} />
139140
<br />
141+
<LocalNotificationsOptionsTest />
142+
<br />
140143
<NotificationChannelsTest notificationType="local" />
141144
</IonContent>
142145
</IonPage>

0 commit comments

Comments
 (0)