-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add Wake Lock Permission with replace configuration in Andro…
…idManifest.xml (#6) * feat: Add WAKE_LOCK permission to AndroidManifest * feat: Add with-enabled-flag and with-permissions plugins * feat: upgrade example expo to 51 and adding fcm * feat: Update README with instructions for running example app * chore: Bump version to 0.1.5 in package.json
- Loading branch information
1 parent
01e5a08
commit b1248ed
Showing
18 changed files
with
1,315 additions
and
1,119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,5 @@ yarn-error.* | |
|
||
ios/ | ||
android/ | ||
google-services.json | ||
GoogleService-Info.plist |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"react-native": { | ||
"messaging_android_notification_channel_id": "high-priority" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { registerRootComponent } from "expo"; | ||
import App from "./src"; | ||
|
||
registerRootComponent(App); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import messaging from "@react-native-firebase/messaging"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useDeviceToken = (enabled: boolean = true) => { | ||
const [deviceToken, setDeviceToken] = useState<string>(); | ||
|
||
useEffect(() => { | ||
messaging() | ||
.registerDeviceForRemoteMessages() | ||
.then(async () => { | ||
const token = await messaging().getToken(); | ||
|
||
if (token) { | ||
console.log("Device token:", token); | ||
setDeviceToken(token); | ||
} | ||
}); | ||
}, [enabled]); | ||
|
||
return deviceToken; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export interface User { | ||
id: number; | ||
login: string; | ||
avatar_url: string; | ||
url: string; | ||
} | ||
|
||
export const getGithubUsers = async (): Promise<User[]> => { | ||
try { | ||
const response = await fetch("https://api.github.com/users"); | ||
const users = await response.json(); | ||
return users as User[]; | ||
} catch (e) { | ||
console.error(e); | ||
return []; | ||
} | ||
}; | ||
|
||
export const useGithubUsers = (enabled: boolean = true) => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
|
||
useEffect(() => { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
getGithubUsers().then((users) => setUsers(users)); | ||
}, [enabled]); | ||
|
||
return users; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import notifee, { AuthorizationStatus } from "@notifee/react-native"; | ||
import messaging from "@react-native-firebase/messaging"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useNotificationPermission() { | ||
const [enabled, setEnabled] = useState(false); | ||
|
||
useEffect(() => { | ||
Promise.all([ | ||
messaging().requestPermission(), | ||
notifee.requestPermission(), | ||
]).then(([authStatus, notifStatus]) => { | ||
const enabled = | ||
authStatus === messaging.AuthorizationStatus.AUTHORIZED || | ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL; | ||
|
||
const localNotificationGranted = | ||
notifStatus.authorizationStatus === AuthorizationStatus.AUTHORIZED; | ||
|
||
console.log("Authorization status:", authStatus); | ||
console.log("Local notification status:", notifStatus); | ||
|
||
setEnabled(enabled && localNotificationGranted); | ||
}); | ||
}, []); | ||
|
||
return enabled; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { FlatList, SafeAreaView, StyleSheet, Text } from "react-native"; | ||
import { useDeviceToken } from "./hooks/use-device-token"; | ||
import { useGithubUsers } from "./hooks/use-github-user"; | ||
import { useNotificationPermission } from "./hooks/use-notification-permission"; | ||
import "./utils/set-background-notification-handler"; | ||
|
||
export default function App() { | ||
const isPermissionGranted = useNotificationPermission(); | ||
const deviceToken = useDeviceToken(isPermissionGranted); | ||
const users = useGithubUsers(isPermissionGranted); | ||
|
||
return ( | ||
<SafeAreaView style={styles.container}> | ||
<Text style={styles.text} selectable> | ||
{isPermissionGranted | ||
? "Notification permission granted" | ||
: "No notification permission"} | ||
</Text> | ||
|
||
<Text style={styles.text} selectable> | ||
{deviceToken ? `Device token: ${deviceToken}` : "No device token"} | ||
</Text> | ||
|
||
<FlatList | ||
data={users} | ||
renderItem={({ item }) => ( | ||
<Text selectable style={[styles.text, { marginBottom: 8 }]}> | ||
{item.login} | ||
</Text> | ||
)} | ||
/> | ||
</SafeAreaView> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: "#fff", | ||
}, | ||
text: { | ||
marginBottom: 16, | ||
marginHorizontal: 16, | ||
fontSize: 14, | ||
textAlign: "left", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import notifee from "@notifee/react-native"; | ||
|
||
export async function createLocalNotification(title: string, body: string) { | ||
// Request permissions (required for iOS) | ||
await notifee.requestPermission(); | ||
|
||
// Create a channel (required for Android) | ||
const channelId = await notifee.createChannel({ | ||
id: "high-priority", | ||
name: "High Priority Notifications", | ||
}); | ||
|
||
// Display a notification | ||
await notifee.displayNotification({ | ||
title, | ||
body, | ||
android: { | ||
channelId, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import messaging from "@react-native-firebase/messaging"; | ||
import { createLocalNotification } from "./create-local-notification"; | ||
|
||
messaging().setBackgroundMessageHandler(async (remoteMessage) => { | ||
console.log("Message handled in the background!", remoteMessage); | ||
|
||
await createLocalNotification( | ||
remoteMessage.notification?.title ?? "No title", | ||
remoteMessage.notification?.body ?? "No body" | ||
); | ||
}); |
Oops, something went wrong.