Skip to content

Commit ce1cf46

Browse files
committed
feat: notification
1 parent 4355ab6 commit ce1cf46

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

example/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<!-- Android 12 and below traditional storage permission -->
77
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
88

9+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
10+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
11+
912
<application
1013
android:name=".MainApplication"
1114
android:label="@string/app_name"

example/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
33
import Navigation from './navigation';
44
import { PlayerProvider } from './contexts/PlayerContext';
55
import { useSetupAudioPro } from './hooks/useSetupAudio';
6+
import { useNotificationPermission } from './hooks/useNotificationPermission';
67

78
function AppContent() {
89
useSetupAudioPro();
10+
useNotificationPermission();
911

1012
return (
1113
<>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useEffect } from 'react';
2+
import { Platform, PermissionsAndroid } from 'react-native';
3+
4+
export function useNotificationPermission() {
5+
useEffect(() => {
6+
const requestNotificationPermission = async () => {
7+
if (Platform.OS === 'android') {
8+
try {
9+
if (Platform.Version >= 33) {
10+
const granted = await PermissionsAndroid.request(
11+
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
12+
{
13+
title: 'Notification Permission',
14+
message:
15+
'Need notification permission to show music playback control',
16+
buttonNeutral: 'Ask later',
17+
buttonNegative: 'Cancel',
18+
buttonPositive: 'OK',
19+
}
20+
);
21+
console.log('Notification permission status:', granted);
22+
}
23+
} catch (err) {
24+
console.warn('Request notification permission failed:', err);
25+
}
26+
}
27+
};
28+
29+
requestNotificationPermission();
30+
}, []);
31+
}

example/src/hooks/useSetupAudio.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,17 @@ export function setupAudioPro(): void {
2424
switch (event.type) {
2525
case AudioProEventType.TRACK_ENDED:
2626
// Auto-play next track when current track ends
27-
const currentTrack = AudioPro.getPlayingTrack();
28-
if (currentTrack) {
29-
const currentIndex = playList.findIndex(
30-
(track) => track.id === currentTrack.id
31-
);
32-
const nextIndex = (currentIndex + 1) % playList.length;
33-
const nextTrack = playList[nextIndex];
34-
if (nextTrack) {
35-
AudioPro.play(nextTrack);
36-
}
37-
}
27+
playNextOrPrev(true);
3828
break;
3929

4030
case AudioProEventType.REMOTE_NEXT:
4131
// Handle next button press from lock screen/notification
42-
const nextTrack = AudioPro.getPlayingTrack();
43-
if (nextTrack) {
44-
AudioPro.play(nextTrack);
45-
}
32+
playNextOrPrev(true);
4633
break;
4734

4835
case AudioProEventType.REMOTE_PREV:
4936
// Handle previous button press from lock screen/notification
50-
const prevTrack = AudioPro.getPlayingTrack();
51-
if (prevTrack) {
52-
AudioPro.play(prevTrack);
53-
}
37+
playNextOrPrev(false);
5438
break;
5539

5640
case AudioProEventType.PLAYBACK_ERROR:
@@ -60,6 +44,22 @@ export function setupAudioPro(): void {
6044
});
6145
}
6246

47+
function playNextOrPrev(isNext: boolean): void {
48+
const currentTrack = AudioPro.getPlayingTrack();
49+
if (currentTrack) {
50+
const currentIndex = playList.findIndex(
51+
(track) => track.id === currentTrack.id
52+
);
53+
const nextIndex = isNext
54+
? (currentIndex + 1) % playList.length
55+
: (currentIndex - 1 + playList.length) % playList.length;
56+
const nextTrack = playList[nextIndex];
57+
if (nextTrack) {
58+
AudioPro.play(nextTrack);
59+
}
60+
}
61+
}
62+
6363
// setup audio pro inside react native lifecycle
6464
export function useSetupAudioPro(): void {
6565
const { playlist } = usePlayer();

0 commit comments

Comments
 (0)