fix push notification with @notifee/react-native lib
This commit is contained in:
parent
6fd1ac61a2
commit
7e8ddbb1be
@ -12,6 +12,7 @@
|
||||
|
||||
#import <Firebase.h>
|
||||
#import "RNSplashScreen.h"
|
||||
#import <RNNotifee/RNNotifee.h>
|
||||
|
||||
#import <FBSDKCoreKit/FBSDKCoreKit.h>
|
||||
|
||||
@ -27,6 +28,11 @@
|
||||
[FIRApp configure];
|
||||
}
|
||||
|
||||
// ตั้งค่า Notifee delegate ก่อน
|
||||
[RNNotifee onBackgroundEvent:^(NSDictionary * _Nonnull event) {
|
||||
// Handle background events
|
||||
}];
|
||||
|
||||
// เพิ่ม notification center delegate
|
||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||
center.delegate = self;
|
||||
@ -81,23 +87,31 @@
|
||||
willPresentNotification:(UNNotification *)notification
|
||||
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
|
||||
|
||||
// ถ้าเป็น Remote Notification (จาก FCM) ให้ซ่อนไว้ (None) เพื่อไม่ให้ซ้ำกับที่ Notifee จะแสดง
|
||||
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
|
||||
completionHandler(UNNotificationPresentationOptionNone);
|
||||
} else {
|
||||
// ถ้าเป็น Local Notification (จาก Notifee) ให้แสดงผล (Alert/Sound/Badge)
|
||||
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
|
||||
// ให้ Notifee จัดการ notification ก่อน
|
||||
if ([RNNotifee willPresentNotification:notification withCompletionHandler:completionHandler]) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ถ้า Notifee ไม่จัดการ ให้แสดงแบบปกติ
|
||||
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
|
||||
}
|
||||
|
||||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
||||
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
||||
withCompletionHandler:(void (^)(void))completionHandler {
|
||||
|
||||
// ให้ Notifee จัดการ response ก่อน
|
||||
if ([RNNotifee didReceiveNotificationResponse:response withCompletionHandler:completionHandler]) {
|
||||
return;
|
||||
}
|
||||
|
||||
completionHandler();
|
||||
}
|
||||
|
||||
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
||||
// ส่ง token ให้ทั้ง Firebase และ Notifee
|
||||
[FIRMessaging messaging].APNSToken = deviceToken;
|
||||
[RNNotifee didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
|
||||
}
|
||||
|
||||
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
||||
|
||||
@ -16,6 +16,7 @@ import IndicatorLoading from '../../components/IndicatorLoading';
|
||||
import { locale, t } from '../../utils/i18n'
|
||||
import moment from "moment";
|
||||
import messaging from "@react-native-firebase/messaging";
|
||||
import NotificationService from '../../utils/NotificationService';
|
||||
|
||||
class LoginScreen extends Component {
|
||||
|
||||
@ -37,6 +38,10 @@ class LoginScreen extends Component {
|
||||
await messaging().deleteToken()
|
||||
console.log('deletetoken');
|
||||
|
||||
// Initialize notification service
|
||||
await NotificationService.initialize();
|
||||
await NotificationService.createNotificationChannel();
|
||||
await NotificationService.setupBackgroundHandler();
|
||||
}
|
||||
|
||||
async _login() {
|
||||
@ -50,45 +55,24 @@ class LoginScreen extends Component {
|
||||
if (regex.test(this.state.username) === true) {
|
||||
console.log('true')
|
||||
try {
|
||||
const authStatus = await messaging().requestPermission({
|
||||
alert: true,
|
||||
sound: true,
|
||||
badge: true,
|
||||
});
|
||||
// ใช้ NotificationService
|
||||
const isPermissionGranted = await NotificationService.initialize();
|
||||
|
||||
console.log('authStatus >>> ', authStatus)
|
||||
const enabled =
|
||||
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
|
||||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
|
||||
if (isPermissionGranted) {
|
||||
const tokens = await NotificationService.getTokens();
|
||||
fcmToken = tokens.fcmToken;
|
||||
|
||||
if (enabled) {
|
||||
console.log('Notification permission granted.');
|
||||
await messaging().registerDeviceForRemoteMessages();
|
||||
|
||||
if (Platform.OS === 'ios') {
|
||||
let apnsToken = await messaging().getAPNSToken();
|
||||
let retry = 0;
|
||||
while (!apnsToken && retry < 10) {
|
||||
console.log('Waiting for APNs token...');
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
apnsToken = await messaging().getAPNSToken();
|
||||
retry++;
|
||||
console.log('FCM Token:', fcmToken);
|
||||
if (tokens.apnsToken) {
|
||||
console.log('APNs Token:', tokens.apnsToken);
|
||||
}
|
||||
console.log('APNs Token:', apnsToken);
|
||||
}
|
||||
|
||||
fcmToken = await messaging().getToken();
|
||||
console.log('fcmToken >>>> ', fcmToken)
|
||||
} else {
|
||||
console.log('Notification permission denied.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to request notification permission:', error);
|
||||
console.error('Failed to get notification tokens:', error);
|
||||
}
|
||||
|
||||
// const fcmToken = await messaging().getAPNSToken()
|
||||
console.log('await messaging().getAPNSToken() <<<<<< ', fcmToken)
|
||||
|
||||
|
||||
let params = {
|
||||
// mobile: this.state.username,
|
||||
@ -98,7 +82,6 @@ class LoginScreen extends Component {
|
||||
}
|
||||
|
||||
console.log('params >> ', params)
|
||||
// console.log('fcmToken >>>>>>>>>>>>>>>>>>>>>>>>> ', fcmToken)
|
||||
this.setState({ isLoading: true })
|
||||
login(params)
|
||||
.then((res) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user