Skip to content

Commit

Permalink
feat: create notificatin token screen
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoonleo committed Apr 5, 2024
1 parent 96ac3ed commit bee8e20
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions front/lib/app/config/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:front/features/task/presentation/task_detail/screen/task_detail_
import 'package:front/features/team/presentation/pages/team/team_create.dart';
import 'package:front/features/team/presentation/pages/team/team_datail.dart';
import 'package:front/features/team/presentation/pages/team/teams_list.dart';
import 'package:front/features/tmp/notificatoin_token_screen.dart';
import 'package:go_router/go_router.dart';

final router = GoRouter(initialLocation: '/', routes: [
Expand Down Expand Up @@ -96,5 +97,9 @@ final router = GoRouter(initialLocation: '/', routes: [
const NoTransitionPage<void>(child: ChangeInfoListScreen()),
),
]),
GoRoute(
path: 'notificationToken',
name: 'notificationToken',
builder: (context, state) => const NotificationTokenScreen()),
]),
]);
2 changes: 2 additions & 0 deletions front/lib/core/const/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ const refreshTokenKey = 'refresh_token';
/// is authenticated key for secure storage
const IS_AUTHENTICATED_KEY = 'IS_AUTHENTICATED_KEY';
const AUTHENTICATED_USER_EMAIL_KEY = 'AUTHENTICATED_USER_EMAIL_KEY';

const NOTIFICATION_TOKEN = 'NOTIFICATION_TOKEN';
5 changes: 5 additions & 0 deletions front/lib/features/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class HomeScreen extends StatelessWidget {
context.go('/myInfo');
},
child: const Text('마이 페이지')),
ElevatedButton(
onPressed: () {
context.go('/notificationToken');
},
child: const Text('알림 토큰 확인')),
])));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ part 'project.g.dart';

@riverpod
class ProjectViewmodel extends _$ProjectViewmodel {

@override
ProjectState build() => const ProjectState.loading();

Expand Down
35 changes: 35 additions & 0 deletions front/lib/features/tmp/notificatoin_token_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:front/core/const/const.dart';
import 'package:shared_preferences/shared_preferences.dart';

class NotificationTokenScreen extends StatefulWidget {
const NotificationTokenScreen({super.key});

@override
State<NotificationTokenScreen> createState() =>
_NotificationTokenScreenState();
}

class _NotificationTokenScreenState extends State<NotificationTokenScreen> {
late String notificationToken;
Future<SharedPreferences> sharedPreferences = SharedPreferences.getInstance();

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FutureBuilder(
future: sharedPreferences,
builder: (context, snapshot) {
notificationToken = snapshot.data?.getString(NOTIFICATION_TOKEN) ??
'error token not generated';
return Padding(
padding: const EdgeInsets.all(24.0),
child: SelectableText(notificationToken),
);
},
),
),
);
}
}
6 changes: 5 additions & 1 deletion front/lib/main/firebase.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:front/core/const/const.dart';
import 'package:front/firebase_options.dart';
import 'package:shared_preferences/shared_preferences.dart';

void setUpFirebaseCloudMessage() async {
WidgetsFlutterBinding.ensureInitialized();

await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

var sharedPreferences = await SharedPreferences.getInstance();
var messaging = FirebaseMessaging.instance;

var settings = await messaging.requestPermission(
Expand All @@ -31,6 +33,8 @@ void setUpFirebaseCloudMessage() async {
debugPrint('User granted permission: ${settings.authorizationStatus}');

var token = await messaging.getToken();
await sharedPreferences.setString(
NOTIFICATION_TOKEN, token ?? 'error token not generated');
debugPrint('Token: $token');

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
Expand Down

0 comments on commit bee8e20

Please sign in to comment.