이 포스팅은 Flutter 3.24 버전 기준으로 작성되었습니다.
Apple developer 계정이 필요합니다. 없다면 Apple Developer↗에 가입, 폭은 이 과정은 패스 후 안드로이드만 진행해주세요
/ios/Runner.xcodeproj/project.pbxproj
파일에 있는 PRODUCT_BUNDLE_IDENTIFIER
값입니다(예시: com.example.promleeblog
).
/ios
폴더에 우클릭 후 Open in Xcode 버튼을 눌러 Xcode로 열어줍니다.
Runner > Runner > Signing & Capabilities
탭으로 이동 후 + Capability
버튼 클릭 후 Push Notifications
를 검색해 추가해줍니다.Signing & Capabilities
탭에서 Background Modes에 다음과 같이 체크해줍니다./ios/Podfile
파일에 다음과 같이 권한을 추가해줍니다. 이미 추가되어있는 경우는 PERMISSION_NOTIFICATIONS
항목이 주석 해제되어있는지만 확인해주시면 됩니다.post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# You can remove unused permissions here
# for more information: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
# e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
# 'PERMISSION_EVENTS=1',
## dart: PermissionGroup.calendarFullAccess
# 'PERMISSION_EVENTS_FULL_ACCESS=1',
## dart: PermissionGroup.reminders
# 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
# 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## The 'PERMISSION_LOCATION' macro enables the `locationWhenInUse` and `locationAlways` permission. If
## the application only requires `locationWhenInUse`, only specify the `PERMISSION_LOCATION_WHENINUSE`
## macro.
##
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
# 'PERMISSION_LOCATION=1',
# 'PERMISSION_LOCATION_WHENINUSE=0',
## dart: PermissionGroup.notification
'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
# 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
# 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_CRITICAL_ALERTS=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_ASSISTANT=1',
]
end
end
end
<manifest ...>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application ...>
...
</application>
</manifest>
flutter pub add firebase_messaging
import 'package:firebase_messaging/firebase_messaging.dart';
// 적절한 위치에 선언
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
백그라운드 메시지 핸들러와 관련하여 유의해야 할 몇 가지 사항이 있습니다. 주의해주세요
import 'package:firebase_messaging/firebase_messaging.dart';
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
import 'package:firebase_messaging/firebase_messaging.dart';
void main(){
// ...
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
runApp(MyApp());
}
import 'package:firebase_messaging/firebase_messaging.dart';
// 적절한 위치에 선언
final req = await FirebaseMessaging.instance.requestPermission(
alert: true,
badge: true,
sound: true,
);
final fcmToken = await FirebaseMessaging.instance.getToken();
if (req.authorizationStatus == AuthorizationStatus.authorized && fcmToken != null) {
print('FCM Token: $fcmToken');
} else {
print('FCM Token: null');
}
먼저 기기에 앱이 설치되어 있어야 합니다. Firebase Console - Cloud Messaging↗에 접속 후 로그인 후 원하는 프로젝트를 선택합니다. 첫번째 캠페인 생성 버튼을 누른 후Firebase 알림 메시지
를 선택 후 만들기 버튼을 눌러 진행합니다.
apns token has not been set yet. please ensure the apns token is available by calling getapnstoken()
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
firebase.getAPNSToken() // 이 코드 추가
await messaging.getToken();
runApp(MyApp());
}