Skip to content

Commit

Permalink
Merge pull request #160 from 4Ailen/fix/#158_build-error
Browse files Browse the repository at this point in the history
fix: ios fcm
  • Loading branch information
urlotus authored Sep 21, 2023
2 parents 51d228d + 1870173 commit 7ff635c
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 19 deletions.
7 changes: 7 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
{
"client_id": "101096502511-egccalvrmfdis0ajrakdk13j45bsv0ko.apps.googleusercontent.com",
"client_type": 3
},
{
"client_id": "101096502511-9u0htr73hlptae6797tq6ricj32oub9a.apps.googleusercontent.com",
"client_type": 2,
"ios_info": {
"bundle_id": "com.aliens.friendship"
}
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
B9B219F261AF02E4B00A9AE7 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4A5D06B50050B06BFB741DD /* Pods_Runner.framework */; };
CBB1FBB73047230BD6A8210A /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = E505E99D9A9D53279DBEA8C0 /* GoogleService-Info.plist */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -49,6 +50,7 @@
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
AD6D5D1B9979D1FB08406FC0 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
E505E99D9A9D53279DBEA8C0 /* GoogleService-Info.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "Runner/GoogleService-Info.plist"; sourceTree = "<group>"; };
F4A5D06B50050B06BFB741DD /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -101,6 +103,7 @@
97C146EF1CF9000F007C117D /* Products */,
569DE7B90B60D17395093927 /* Pods */,
6289FEA03F3A6C02FD605D2F /* Frameworks */,
E505E99D9A9D53279DBEA8C0 /* GoogleService-Info.plist */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -197,6 +200,7 @@
022A3EC42A4D9ABC004DECC8 /* GoogleService-Info.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
CBB1FBB73047230BD6A8210A /* GoogleService-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 4 additions & 1 deletion ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import FirebaseCore
) -> Bool {
FirebaseApp.configure()

UNUserNotificationCenter.current().delegate = self
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}


let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
Expand Down
4 changes: 4 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>NSBonjourServices</key>
<array>
<string>_dartobservatory._tcp</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
Expand Down
7 changes: 7 additions & 0 deletions ios/firebase_app_id_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"file_generated_by": "FlutterFire CLI",
"purpose": "FirebaseAppID & ProjectID for this Firebase app in this directory",
"GOOGLE_APP_ID": "1:101096502511:ios:6c8238b88c31d399bad71a",
"FIREBASE_PROJECT_ID": "friendship-aefec",
"GCM_SENDER_ID": "101096502511"
}
65 changes: 54 additions & 11 deletions lib/apis/firebase_apis.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'dart:convert';

import '../firebase_options.dart';
import '../repository/sql_message_database.dart';


Expand All @@ -14,25 +18,65 @@ import '../repository/sql_message_database.dart';
//FlutterLocalNotificationsPlugin 패키지 초기화
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

late AndroidNotificationChannel channel;
bool isFlutterLocalNotificationsInitialized = false;

class FirebaseAPIs {

static Future<void> setupFlutterNotifications() async {
if (isFlutterLocalNotificationsInitialized) {
return;
}
channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.', // description
importance: Importance.high,
);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);

// 토큰 요청
getToken();

isFlutterLocalNotificationsInitialized = true;
}
static Future<void> getToken() async {
if (Platform.isIOS) {
print('ios');
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true,badge: true,sound: true,);
String? apnsToken = await FirebaseMessaging.instance.getAPNSToken();
print("apnsToken: " + apnsToken.toString());
}

await FirebaseMessaging.instance.getToken().then((value) {
print("deviceToken: " + value.toString());
})!;
}

static final storage = FlutterSecureStorage();
final _firebaseMessaging = FirebaseMessaging.instance;

/// 상단 알림을 위해 AndroidNotificationChannel 생성
static AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
importance: Importance.min,
);

@pragma('vm:entry-point')
static Future<void> FCMBackgroundHandler(RemoteMessage message) async {


await Firebase.initializeApp();
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform,);
await setupFlutterNotifications();

var initialzationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');

Expand All @@ -42,7 +86,6 @@ class FirebaseAPIs {
requestAlertPermission: true,
);


await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
Expand Down
69 changes: 69 additions & 0 deletions lib/firebase_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;

/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for web - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.windows:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for windows - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.linux:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for linux - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
default:
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
}

static const FirebaseOptions android = FirebaseOptions(
apiKey: 'AIzaSyAKLBF_jhn8bE-AAQiC8o4Cyt5apxFr0H0',
appId: '1:101096502511:android:002dc0966418e87fbad71a',
messagingSenderId: '101096502511',
projectId: 'friendship-aefec',
storageBucket: 'friendship-aefec.appspot.com',
);

static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'AIzaSyA1VEVZbiXsmfsu8E8Ul4Lj3AgGT9dU8Do',
appId: '1:101096502511:ios:6c8238b88c31d399bad71a',
messagingSenderId: '101096502511',
projectId: 'friendship-aefec',
storageBucket: 'friendship-aefec.appspot.com',
iosClientId: '101096502511-9u0htr73hlptae6797tq6ricj32oub9a.apps.googleusercontent.com',
iosBundleId: 'com.aliens.friendship',
);
}
64 changes: 58 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:aliens/providers/bookmarks_provider.dart';
import 'package:aliens/providers/comment_provider.dart';
import 'package:aliens/providers/market_comment_provider.dart';
Expand Down Expand Up @@ -67,15 +69,64 @@ import './views/pages/splash_page.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_core/firebase_core.dart';

import 'apis/firebase_apis.dart';
import './apis/firebase_apis.dart';
import 'package:overlay_support/overlay_support.dart';

import 'firebase_options.dart';
//import 'firebase_options.dart';

final supportedLocales = [
Locale('en', 'US'),
Locale('ko', 'KR')
];

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('백그라운드 도착1');
}

@pragma('vm:entry-point')
void backgroundHandler(NotificationResponse details) {
print('백그라운드 도착2');
}

void initializeNotification() async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(const AndroidNotificationChannel(
'high_importance_channel', 'high_importance_notification',
importance: Importance.max));

await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
iOS: DarwinInitializationSettings(
requestSoundPermission: true,
requestBadgePermission: true,
requestAlertPermission: true,
),
),
onDidReceiveNotificationResponse: (details) {
print(details);
},
onDidReceiveBackgroundNotificationResponse: backgroundHandler,
);

await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);

RemoteMessage? message = await FirebaseMessaging.instance.getInitialMessage();
if (message != null) {
print(message);
}
}

void main() async {

WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -86,13 +137,14 @@ void main() async {


// fcm 초기화 부분
await initializeDefault();
final fcmToken = await FirebaseMessaging.instance.getToken();
// fcm 토큰 출력
print(fcmToken);
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,);

FirebaseMessaging.onBackgroundMessage(FirebaseAPIs.FCMBackgroundHandler); // 백그라운드에서 동작하게 해줌
await FirebaseAPIs.setupFlutterNotifications();
FirebaseMessaging.onBackgroundMessage(FirebaseAPIs.FCMBackgroundHandler);

flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
initializeNotification();

runApp(MultiProvider(
providers: [
Expand Down
2 changes: 1 addition & 1 deletion lib/views/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:image_picker/image_picker.dart';
import 'package:overlay_support/overlay_support.dart';

import '../../../apis/apis.dart';
import '../../apis/firebase_apis.dart';
import '../../main.dart';
import '../../mockdatas/mockdata_model.dart';
import '../../models/screenArgument.dart';
Expand Down Expand Up @@ -64,7 +65,6 @@ class _HomePageState extends State<HomePage> {
var inAppNotification = await storage.read(key: 'inAppNotification');

if(message.data['type']=='ARTICLE_LIKE'){

print(json.decode(inAppNotification!)['inAppNotification']);
if(json.decode(inAppNotification!)['inAppNotification'] == true){
showOverlayNotification((context) {
Expand Down

0 comments on commit 7ff635c

Please sign in to comment.