Merge pull request 'Penerapan ke database' (#31) from notification/16-dec-25 into staging
Reviewed-on: http://wibugit.wibudev.com/wibu/hipmi-mobile/pulls/31
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
<data android:scheme="https"/>
|
<data android:scheme="https"/>
|
||||||
</intent>
|
</intent>
|
||||||
</queries>
|
</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_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="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"/>
|
<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>
|
<StackCustom>
|
||||||
<ButtonCustom onPress={() => router.push("./test-notifications")}>Test Notif</ButtonCustom>
|
{/* <ButtonCustom onPress={() => router.push("./test-notifications")}>Test Notif</ButtonCustom> */}
|
||||||
|
|
||||||
<Home_ImageSection />
|
<Home_ImageSection />
|
||||||
|
|
||||||
|
|||||||
@@ -2,27 +2,96 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import messaging from "@react-native-firebase/messaging";
|
import messaging from "@react-native-firebase/messaging";
|
||||||
import { useForegroundNotifications } from "@/hooks/use-foreground-notifications";
|
import { useForegroundNotifications } from "@/hooks/use-foreground-notifications";
|
||||||
import {
|
import { useNotificationStore } from "@/hooks/use-notification-store";
|
||||||
useNotificationStore,
|
|
||||||
} from "@/hooks/use-notification-store";
|
|
||||||
import type { FirebaseMessagingTypes } from "@react-native-firebase/messaging";
|
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() {
|
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(() => {
|
useEffect(() => {
|
||||||
const getFCMToken = async () => {
|
if (!user) {
|
||||||
if (!messaging().isSupported()) return;
|
console.log("User not available, skipping token sync");
|
||||||
const authStatus = await messaging().requestPermission();
|
return;
|
||||||
if (authStatus === messaging.AuthorizationStatus.AUTHORIZED) {
|
}
|
||||||
const token = await messaging().getToken();
|
|
||||||
console.log("✅ FCM Token:", token);
|
// 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
|
registerDeviceToken();
|
||||||
const { addNotification } = useNotificationStore();
|
}, [user?.id]);
|
||||||
|
|
||||||
const handleForegroundNotification = (
|
const handleForegroundNotification = (
|
||||||
message: FirebaseMessagingTypes.RemoteMessage
|
message: FirebaseMessagingTypes.RemoteMessage
|
||||||
@@ -33,9 +102,10 @@ export default function NotificationInitializer() {
|
|||||||
|
|
||||||
const safeData: Record<string, string> = {};
|
const safeData: Record<string, string> = {};
|
||||||
for (const key in rawData) {
|
for (const key in rawData) {
|
||||||
safeData[key] = typeof rawData[key] === "string"
|
safeData[key] =
|
||||||
? rawData[key]
|
typeof rawData[key] === "string"
|
||||||
: JSON.stringify(rawData[key]);
|
? rawData[key]
|
||||||
|
: JSON.stringify(rawData[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("📥 Menambahkan ke store:", { title, body, safeData });
|
console.log("📥 Menambahkan ke store:", { title, body, safeData });
|
||||||
|
|||||||
@@ -458,7 +458,7 @@
|
|||||||
);
|
);
|
||||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
|
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG";
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
||||||
PRODUCT_NAME = HIPMIBadungConnect;
|
PRODUCT_NAME = "HIPMIBadungConnect";
|
||||||
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
||||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||||
SWIFT_VERSION = 5.0;
|
SWIFT_VERSION = 5.0;
|
||||||
@@ -490,7 +490,7 @@
|
|||||||
);
|
);
|
||||||
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
|
OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE";
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
PRODUCT_BUNDLE_IDENTIFIER = "com.anonymous.hipmi-mobile";
|
||||||
PRODUCT_NAME = HIPMIBadungConnect;
|
PRODUCT_NAME = "HIPMIBadungConnect";
|
||||||
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
SWIFT_OBJC_BRIDGING_HEADER = "HIPMIBadungConnect/HIPMIBadungConnect-Bridging-Header.h";
|
||||||
SWIFT_VERSION = 5.0;
|
SWIFT_VERSION = 5.0;
|
||||||
TARGETED_DEVICE_FAMILY = "1,2";
|
TARGETED_DEVICE_FAMILY = "1,2";
|
||||||
|
|||||||
@@ -55,6 +55,8 @@
|
|||||||
</dict>
|
</dict>
|
||||||
<key>NSCameraUsageDescription</key>
|
<key>NSCameraUsageDescription</key>
|
||||||
<string>Allow $(PRODUCT_NAME) to access your camera</string>
|
<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>
|
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
|
||||||
<string>Allow $(PRODUCT_NAME) to access your location</string>
|
<string>Allow $(PRODUCT_NAME) to access your location</string>
|
||||||
<key>NSLocationAlwaysUsageDescription</key>
|
<key>NSLocationAlwaysUsageDescription</key>
|
||||||
|
|||||||
@@ -1,23 +1,51 @@
|
|||||||
import { apiConfig } from "./api-config";
|
import { apiConfig } from "./api-config";
|
||||||
|
|
||||||
|
|
||||||
type NotificationProp = {
|
type NotificationProp = {
|
||||||
fcmToken: string
|
fcmToken: string;
|
||||||
title: string,
|
title: string;
|
||||||
body: Object
|
body: Object;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export async function apiNotificationsSend({
|
||||||
export async function apiNotificationsSend({ data }: { data: NotificationProp }) {
|
data,
|
||||||
|
}: {
|
||||||
|
data: NotificationProp;
|
||||||
|
}) {
|
||||||
try {
|
try {
|
||||||
const response = await apiConfig.post(`/mobile/notifications`, {
|
const response = await apiConfig.post(`/mobile/notifications`, {
|
||||||
data: data,
|
data: data,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Fecth Notif", response.data)
|
console.log("Fecth Notif", response.data);
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw 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