728x90
// AppDelegate.h
UserNotifications을 import하고 인터페이스에 UNUserNotificationCenterDelegate를 추가한다
#import <React/RCTBridgeDelegate.h>
#import <UIKit/UIKit.h>
#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
// AppDelegate.mm
파이어베이스 설정 초기화 후에 delegate를 등록하는 코드 추가
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// [FIRApp configure];
// Add me --- \/
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}
// Add me --- /\
(...)
[self initializeRemoteNotification];
(...)
}
#pragma mark - Initialize Remote Notification
- (void)initializeRemoteNotification {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
}
그리고 마찬가지로 AppDelegate.mm에 아래 소스를 추가한다.
// Receive displayed notifications for iOS 10 devices.
// Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// ...
// Print full message.
NSLog(@"%@", userInfo);
// Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if (userInfo) {
NSLog(@"Message ID: %@", userInfo);
}
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Print full message.
NSLog(@"%@", userInfo);
completionHandler();
}
핸들러 param에는 원하는 프레젠테이션 옵션을 넣어주면 된다 (배너,배지,사운드)
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
위와같이 코드를 추가해주면 알람을 받았을 때 앱 실행상태에서도 푸시가 표시되는 걸 확인할 수 있다.
'Mobile App Develop > iOS개발' 카테고리의 다른 글
[Xcode] iOS Launch Screen 추가하기 (0) | 2023.01.17 |
---|---|
[RN] PhaseScriptExecution failed with a nonzero exit code 에러 해결법 (0) | 2023.01.17 |
iOS앱 업데이트 심사 (0) | 2022.07.06 |
This operation can fail if the version of the OS on the device is incompatible with the installed version of Xcode. 오류 뜰 시 (0) | 2022.07.05 |
[iOS개발] 리액트네이티브 웹뷰 - pod install 안될때 (0) | 2022.06.21 |