upd: push notifikasi
Deskripsi: - blm selesai No Issues
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
apply plugin: "com.android.application"
|
apply plugin: "com.android.application"
|
||||||
apply plugin: "org.jetbrains.kotlin.android"
|
apply plugin: "org.jetbrains.kotlin.android"
|
||||||
apply plugin: "com.facebook.react"
|
apply plugin: "com.facebook.react"
|
||||||
apply from: new File(["node", "--print", "require.resolve('expo-modules-core/package.json')"].execute(null, rootDir).text.trim(), "../gradle.groovy")
|
// apply from: new File(["node", "--print", "require.resolve('expo-modules-core/package.json')"].execute(null, rootDir).text.trim(), "../gradle.groovy")
|
||||||
apply from: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), "../react.gradle")
|
// apply from: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), "../react.gradle")
|
||||||
apply from: new File(["node", "--print", "require.resolve('expo-updates/package.json')"].execute(null, rootDir).text.trim(), "../scripts/create-manifest-android.gradle")
|
// apply from: new File(["node", "--print", "require.resolve('expo-updates/package.json')"].execute(null, rootDir).text.trim(), "../scripts/create-manifest-android.gradle")
|
||||||
|
|
||||||
def projectRoot = rootDir.getAbsoluteFile().getParentFile().getAbsolutePath()
|
def projectRoot = rootDir.getAbsoluteFile().getParentFile().getAbsolutePath()
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ useExpoModules()
|
|||||||
include ':app'
|
include ':app'
|
||||||
includeBuild(new File(["node", "--print", "require.resolve('@react-native/gradle-plugin/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim()).getParentFile())
|
includeBuild(new File(["node", "--print", "require.resolve('@react-native/gradle-plugin/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim()).getParentFile())
|
||||||
|
|
||||||
apply from: new File(["node", "--print", "require.resolve('expo-modules-core/package.json')"].execute(null, rootDir).text.trim(), "../gradle.groovy");
|
// apply from: new File(["node", "--print", "require.resolve('expo-modules-core/package.json')"].execute(null, rootDir).text.trim(), "../gradle.groovy");
|
||||||
includeUnimodulesProjects()
|
// includeUnimodulesProjects()
|
||||||
|
|
||||||
apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json')"].execute(null, rootDir).text.trim(), "../native_modules.gradle");
|
// apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json')"].execute(null, rootDir).text.trim(), "../native_modules.gradle");
|
||||||
applyNativeModulesSettingsGradle(settings)
|
// applyNativeModulesSettingsGradle(settings)
|
||||||
|
|||||||
@@ -11,10 +11,20 @@ import store from "@/lib/store";
|
|||||||
import { useAuthSession } from "@/providers/AuthProvider";
|
import { useAuthSession } from "@/providers/AuthProvider";
|
||||||
import { Redirect, router, Stack } from "expo-router";
|
import { Redirect, router, Stack } from "expo-router";
|
||||||
import { StatusBar } from 'expo-status-bar';
|
import { StatusBar } from 'expo-status-bar';
|
||||||
import { Text } from "react-native";
|
import { useEffect } from "react";
|
||||||
|
import { Alert, Text } from "react-native";
|
||||||
import { Provider } from "react-redux";
|
import { Provider } from "react-redux";
|
||||||
|
import messaging from "@react-native-firebase/messaging";
|
||||||
|
|
||||||
export default function RootLayout() {
|
export default function RootLayout() {
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = messaging().onMessage(async remoteMessage => {
|
||||||
|
Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
|
||||||
|
});
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
}, []);
|
||||||
|
|
||||||
const { token, isLoading } = useAuthSession()
|
const { token, isLoading } = useAuthSession()
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import { useAuthSession } from "@/providers/AuthProvider";
|
|||||||
import { Redirect } from "expo-router";
|
import { Redirect } from "expo-router";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Text } from "react-native";
|
import { Text } from "react-native";
|
||||||
|
import { useNotification } from "@/lib/useNotification";
|
||||||
|
|
||||||
export default function Index() {
|
export default function Index() {
|
||||||
|
useNotification()
|
||||||
const [isValid, setValid] = useState(false)
|
const [isValid, setValid] = useState(false)
|
||||||
const [phone, setPhone] = useState('')
|
const [phone, setPhone] = useState('')
|
||||||
const [otp, setOtp] = useState(0)
|
const [otp, setOtp] = useState(0)
|
||||||
|
|||||||
78
lib/useNotification.ts
Normal file
78
lib/useNotification.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import firebase from '@react-native-firebase/app';
|
||||||
|
import messaging from '@react-native-firebase/messaging';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { PermissionsAndroid } from 'react-native';
|
||||||
|
|
||||||
|
// Your Firebase project configuration
|
||||||
|
const RNfirebaseConfig = {
|
||||||
|
apiKey: "AIzaSyB2hbsW91J3oRQx4_jgrCCNY0tNt5-21e8",
|
||||||
|
authDomain: "googleapis.com",
|
||||||
|
projectId: "mobile-darmasaba",
|
||||||
|
storageBucket: "mobile-darmasaba.appspot.com",
|
||||||
|
messagingSenderId: "867439221179",
|
||||||
|
appId: "1:867439221179:android:4509f77478c8dce99b0c9e",
|
||||||
|
databaseURL: "https://mobile-darmasaba-default-rtdb.asia-southeast1.firebasedatabase.app/"
|
||||||
|
};
|
||||||
|
|
||||||
|
let initialized = false;
|
||||||
|
|
||||||
|
const initializeFirebase = async () => {
|
||||||
|
if (initialized) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(!firebase.apps.length){
|
||||||
|
firebase.initializeApp(RNfirebaseConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set auto initialization and background message handler
|
||||||
|
messaging().setAutoInitEnabled(true);
|
||||||
|
messaging().setBackgroundMessageHandler(async remoteMessage => {
|
||||||
|
console.log('Message handled in the background!', remoteMessage);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize Firebase:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestPermission = async () => {
|
||||||
|
try {
|
||||||
|
const granted = await PermissionsAndroid.request(
|
||||||
|
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
||||||
|
);
|
||||||
|
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
|
||||||
|
console.log('Notification permission granted');
|
||||||
|
} else {
|
||||||
|
console.log('Notification permission denied');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Error requesting notification permissions:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getToken = async () => {
|
||||||
|
try {
|
||||||
|
const token = await messaging().getToken();
|
||||||
|
console.log('Token:', token);
|
||||||
|
return token;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error getting token:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useNotification = () => {
|
||||||
|
useEffect(() => {
|
||||||
|
const initializeAndSetup = async () => {
|
||||||
|
try {
|
||||||
|
await initializeFirebase();
|
||||||
|
await requestPermission();
|
||||||
|
await getToken();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to setup notifications:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initializeAndSetup();
|
||||||
|
}, []);
|
||||||
|
};
|
||||||
@@ -15,9 +15,12 @@
|
|||||||
"preset": "jest-expo"
|
"preset": "jest-expo"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@expo/server": "^0.6.2",
|
||||||
"@expo/vector-icons": "^14.0.2",
|
"@expo/vector-icons": "^14.0.2",
|
||||||
"@react-native-async-storage/async-storage": "^2.1.2",
|
"@react-native-async-storage/async-storage": "^2.1.2",
|
||||||
"@react-native-community/datetimepicker": "8.2.0",
|
"@react-native-community/datetimepicker": "8.2.0",
|
||||||
|
"@react-native-firebase/app": "^22.2.1",
|
||||||
|
"@react-native-firebase/messaging": "^22.2.1",
|
||||||
"@react-navigation/bottom-tabs": "^7.2.0",
|
"@react-navigation/bottom-tabs": "^7.2.0",
|
||||||
"@react-navigation/native": "^7.0.14",
|
"@react-navigation/native": "^7.0.14",
|
||||||
"@reduxjs/toolkit": "^2.7.0",
|
"@reduxjs/toolkit": "^2.7.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user