Compare commits
1 Commits
notificati
...
notificati
| Author | SHA1 | Date | |
|---|---|---|---|
| 05c1cac10f |
@@ -15,7 +15,7 @@
|
||||
<data android:scheme="https"/>
|
||||
</intent>
|
||||
</queries>
|
||||
<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme" android:supportsRtl="true" android:enableOnBackInvokedCallback="false">
|
||||
<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme" android:supportsRtl="true" android:enableOnBackInvokedCallback="false" android:fullBackupContent="@xml/secure_store_backup_rules" android:dataExtractionRules="@xml/secure_store_data_extraction_rules">
|
||||
<meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/notification_icon_color"/>
|
||||
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/notification_icon"/>
|
||||
<meta-data android:name="expo.modules.notifications.default_notification_color" android:resource="@color/notification_icon_color"/>
|
||||
|
||||
@@ -145,7 +145,7 @@ export default function Application() {
|
||||
}
|
||||
>
|
||||
<StackCustom>
|
||||
<ButtonCustom onPress={() => router.push("./test-notifications")}>Test Notif</ButtonCustom>
|
||||
{/* <ButtonCustom onPress={() => router.push("./test-notifications")}>Test Notif</ButtonCustom> */}
|
||||
|
||||
<Home_ImageSection />
|
||||
|
||||
|
||||
@@ -2,27 +2,96 @@
|
||||
import { useEffect } from "react";
|
||||
import messaging from "@react-native-firebase/messaging";
|
||||
import { useForegroundNotifications } from "@/hooks/use-foreground-notifications";
|
||||
import {
|
||||
useNotificationStore,
|
||||
} from "@/hooks/use-notification-store";
|
||||
import { useNotificationStore } from "@/hooks/use-notification-store";
|
||||
import type { FirebaseMessagingTypes } from "@react-native-firebase/messaging";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
import { Platform } from "react-native";
|
||||
import * as Device from "expo-device";
|
||||
import * as Application from "expo-application";
|
||||
import { apiDeviceRegisterToken } from "@/service/api-notifications";
|
||||
|
||||
|
||||
export default function NotificationInitializer() {
|
||||
// 1. Ambil token FCM (opsional, hanya untuk log)
|
||||
// Setup handler notifikasi
|
||||
const { user } = useAuth(); // dari AuthContext
|
||||
const { addNotification } = useNotificationStore();
|
||||
|
||||
// Ambil token FCM (opsional, hanya untuk log)
|
||||
useEffect(() => {
|
||||
const getFCMToken = async () => {
|
||||
if (!messaging().isSupported()) return;
|
||||
const authStatus = await messaging().requestPermission();
|
||||
if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) {
|
||||
const token = await messaging().getToken();
|
||||
console.log("✅ FCM Token:", token);
|
||||
if (!user) {
|
||||
console.log("User not available, skipping token sync");
|
||||
return;
|
||||
}
|
||||
|
||||
// if (user?.id) {
|
||||
// syncDeviceToken({ userId: user.id });
|
||||
// }
|
||||
|
||||
const registerDeviceToken = async () => {
|
||||
try {
|
||||
// 1. Minta izin & ambil FCM token
|
||||
if (!messaging().isSupported()) return;
|
||||
const authStatus = await messaging().requestPermission();
|
||||
if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) {
|
||||
const token = await messaging().getToken();
|
||||
console.log("✅ FCM Token:", token);
|
||||
} else {
|
||||
console.warn("Izin notifikasi ditolak");
|
||||
return;
|
||||
}
|
||||
const fcmToken = await messaging().getToken();
|
||||
if (!fcmToken) {
|
||||
console.warn("Gagal mendapatkan FCM token");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Ambil info device
|
||||
const platform = Platform.OS; // "ios" | "android"
|
||||
// ? await Device.getUdid()
|
||||
// : "unknown";
|
||||
const model = Device.modelName || "unknown";
|
||||
const appVersion =
|
||||
(Application.nativeApplicationVersion || "unknown") +
|
||||
"-" +
|
||||
(Application.nativeBuildVersion || "unknown");
|
||||
const deviceId =
|
||||
Device.osInternalBuildId || Device.modelName + "-" + Date.now();
|
||||
|
||||
console.log(
|
||||
"📱 Device info:",
|
||||
JSON.stringify(
|
||||
{
|
||||
fcmToken,
|
||||
platform,
|
||||
deviceId,
|
||||
model,
|
||||
appVersion,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
|
||||
// 3. Kirim ke backend
|
||||
await apiDeviceRegisterToken({
|
||||
data: {
|
||||
fcmToken,
|
||||
platform,
|
||||
deviceId,
|
||||
model,
|
||||
appVersion,
|
||||
userId: user?.id || "",
|
||||
},
|
||||
});
|
||||
|
||||
console.log("✅ Device token berhasil didaftarkan ke backend");
|
||||
} catch (error) {
|
||||
console.error("❌ Gagal mendaftarkan device token:", error);
|
||||
}
|
||||
};
|
||||
getFCMToken();
|
||||
}, []);
|
||||
|
||||
// 2. Setup handler notifikasi
|
||||
const { addNotification } = useNotificationStore();
|
||||
registerDeviceToken();
|
||||
}, [user?.id]);
|
||||
|
||||
const handleForegroundNotification = (
|
||||
message: FirebaseMessagingTypes.RemoteMessage
|
||||
@@ -33,9 +102,10 @@ export default function NotificationInitializer() {
|
||||
|
||||
const safeData: Record<string, string> = {};
|
||||
for (const key in rawData) {
|
||||
safeData[key] = typeof rawData[key] === "string"
|
||||
? rawData[key]
|
||||
: JSON.stringify(rawData[key]);
|
||||
safeData[key] =
|
||||
typeof rawData[key] === "string"
|
||||
? rawData[key]
|
||||
: JSON.stringify(rawData[key]);
|
||||
}
|
||||
|
||||
console.log("📥 Menambahkan ke store:", { title, body, safeData });
|
||||
@@ -46,4 +116,4 @@ export default function NotificationInitializer() {
|
||||
useForegroundNotifications(handleForegroundNotification);
|
||||
|
||||
return null; // komponen ini tidak merender apa-apa
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,7 +458,7 @@
|
||||
);
|
||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
||||
PRODUCT_NAME = HIPMIBadungConnect;
|
||||
PRODUCT_NAME = "HIPMIBadungConnect";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 5.0;
|
||||
@@ -490,7 +490,7 @@
|
||||
);
|
||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
||||
PRODUCT_NAME = HIPMIBadungConnect;
|
||||
PRODUCT_NAME = "HIPMIBadungConnect";
|
||||
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
|
||||
@@ -55,6 +55,8 @@
|
||||
</dict>
|
||||
<key>NSCameraUsageDescription</key>
|
||||
<string>Allow $(PRODUCT_NAME) to access your camera</string>
|
||||
<key>NSFaceIDUsageDescription</key>
|
||||
<string>Allow $(PRODUCT_NAME) to access your Face ID biometric data.</string>
|
||||
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
|
||||
<string>Allow $(PRODUCT_NAME) to access your location</string>
|
||||
<key>NSLocationAlwaysUsageDescription</key>
|
||||
|
||||
@@ -1,23 +1,51 @@
|
||||
import { apiConfig } from "./api-config";
|
||||
|
||||
|
||||
type NotificationProp = {
|
||||
fcmToken: string
|
||||
title: string,
|
||||
body: Object
|
||||
}
|
||||
fcmToken: string;
|
||||
title: string;
|
||||
body: Object;
|
||||
};
|
||||
|
||||
|
||||
export async function apiNotificationsSend({ data }: { data: NotificationProp }) {
|
||||
export async function apiNotificationsSend({
|
||||
data,
|
||||
}: {
|
||||
data: NotificationProp;
|
||||
}) {
|
||||
try {
|
||||
const response = await apiConfig.post(`/mobile/notifications`, {
|
||||
data: data,
|
||||
});
|
||||
|
||||
console.log("Fecth Notif", response.data)
|
||||
console.log("Fecth Notif", response.data);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
type DeviceTokenData = {
|
||||
fcmToken: string;
|
||||
platform: string;
|
||||
deviceId: string;
|
||||
model: string;
|
||||
appVersion: string;
|
||||
userId: string;
|
||||
};
|
||||
|
||||
export async function apiDeviceRegisterToken({
|
||||
data,
|
||||
}: {
|
||||
data: DeviceTokenData;
|
||||
}) {
|
||||
try {
|
||||
const response = await apiConfig.post(`/mobile/auth/device-tokens`, {
|
||||
data: data,
|
||||
});
|
||||
console.log("Device token registered:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Failed to register device token:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user