Fakultas Ilmu Komputer UI

Commit 982883b5 authored by Yoga Pratama's avatar Yoga Pratama
Browse files

[GREEN] Implement fcm getToken()

parent 528e06c9
......@@ -83,7 +83,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-analytics:'
implementation 'com.google.firebase:firebase-messaging:'
implementation 'com.google.firebase:firebase-bom:27.0.0'
}
apply plugin: 'com.google.gms.google-services'
\ No newline at end of file
......@@ -33,6 +33,10 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
......
import 'package:bisaGo/repository/cloud_messaging_repository.dart';
import 'package:bisaGo/repository/kegiatan_repository.dart';
import 'package:bisaGo/repository/kegiatan_terdekat_repository.dart';
import 'package:bisaGo/repository/komentar_posting_kegiatan_repository.dart';
......@@ -26,8 +27,8 @@ class AppGetIt {
() => KomentarPostingRepository());
_getIt.registerLazySingleton<BaseKomentarPostingKegiatanRepository>(
() => KomentarPostingKegiatanRepository());
_getIt.registerLazySingleton<BaseLokasiRepository>(
() => LokasiRepository());
_getIt
.registerLazySingleton<BaseLokasiRepository>(() => LokasiRepository());
_getIt.registerLazySingleton<BaseLayananRepository>(
() => LayananRepository());
_getIt.registerLazySingleton<BaseKegiatanRepository>(
......@@ -36,5 +37,9 @@ class AppGetIt {
() => KegiatanTerdekatRepository());
_getIt.registerLazySingleton<BaseDynamicLinksServiceRepository>(
() => DynamicLinksServiceRepository());
_getIt.registerLazySingleton<BaseDynamicLinksServiceRepository>(
() => DynamicLinksServiceRepository());
_getIt.registerLazySingleton<BaseCloudMessagingRepository>(
() => CloudMessagingRepository());
}
}
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:bisaGo/app.dart';
import 'package:intl/date_symbol_data_local.dart';
......@@ -7,11 +9,20 @@ import 'flavor/flavor.dart';
import 'globalnetwork.dart';
import 'package:bisaGo/get_it.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print('Handling a background message: ${message.messageId}');
}
Future main() async {
AppGetIt().initialize();
await DotEnv().load('.env');
getDioInstance('build');
await initializeDateFormatting('id_ID', null);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
Intl.defaultLocale = 'id_ID';
dio.options.receiveTimeout = 15000;
ApiFlavor.flavor = BuildFlavor.development.toString();
......
......@@ -11,7 +11,9 @@ import 'package:bisaGo/page/filter_fasilitas/postingan/detail_post.dart';
import 'package:bisaGo/repository/komentar_repository.dart';
import 'package:bisaGo/utils/custom_dashboard_location_button.dart';
import 'package:bisaGo/utils/location_turn_on_dialog.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:geolocator/geolocator.dart';
......@@ -38,6 +40,10 @@ class DashboardState extends State<Dashboard> {
LokasiResponseBloc bloc = LokasiResponseBloc();
KegiatanTerdekatBloc blocKegiatanTerdekat = KegiatanTerdekatBloc();
FirebaseMessaging _firebaseMessaging;
bool _initialized = false;
@override
void initState() {
super.initState();
......@@ -45,6 +51,31 @@ class DashboardState extends State<Dashboard> {
geolocator = Geolocator()..forceAndroidLocationManager;
initDynamicLinks();
setInitialLocation();
if (!_initialized) {
_initialized = true;
_setupFirebase();
}
}
void _setupFirebase() async {
await Firebase.initializeApp();
_firebaseMessaging = FirebaseMessaging.instance;
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}');
}
});
_requestFCMToken();
}
void _requestFCMToken() async {
final token = await _firebaseMessaging.getToken();
print(token);
}
void _navigateToPencarianPage(BuildContext context) {
......@@ -368,20 +399,20 @@ class DashboardState extends State<Dashboard> {
..name = namaLokasi;
final fasilitasRoute = MaterialPageRoute(
builder: (BuildContext context) => DetailPostKegiatanPage(
lokasi: lokasi,
kegiatan: KegiatanModel(
id: kegiatan.id,
placeId: lokasi.placeId,
creator: kegiatan.creator,
namaKegiatan: kegiatan.namaKegiatan,
penyelenggara: kegiatan.penyelenggara,
narahubung: kegiatan.narahubung,
deskripsi: kegiatan.deskripsi,
timeStart: kegiatan.timeStart,
timeEnd: kegiatan.timeEnd,
image: kegiatan.image,
),
));
lokasi: lokasi,
kegiatan: KegiatanModel(
id: kegiatan.id,
placeId: lokasi.placeId,
creator: kegiatan.creator,
namaKegiatan: kegiatan.namaKegiatan,
penyelenggara: kegiatan.penyelenggara,
narahubung: kegiatan.narahubung,
deskripsi: kegiatan.deskripsi,
timeStart: kegiatan.timeStart,
timeEnd: kegiatan.timeEnd,
image: kegiatan.image,
),
));
await Navigator.of(context).push(fasilitasRoute);
}
......
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:bisaGo/flavor/flavor.dart';
abstract class BaseCloudMessagingRepository {
Future<void> sendFCMToken(String fcmToken, String token);
}
......@@ -5,6 +10,15 @@ abstract class BaseCloudMessagingRepository {
class CloudMessagingRepository implements BaseCloudMessagingRepository {
@override
Future<bool> sendFCMToken(String fcmToken, String token) async {
return false;
try {
await http.post(
'${ApiFlavor.getBaseUrl()}/',
headers: {'Authorization': token, 'content-type': 'application/json'},
body: json.encode({'token': fcmToken}),
);
return true;
} catch (_) {
return false;
}
}
}
......@@ -55,6 +55,7 @@ dependencies:
firebase_core: ^0.7.0
firebase_core_platform_interface: ^3.0.1
firebase_dynamic_links: ^0.7.0+1
firebase_messaging: ^8.0.0-dev.15
carousel_slider: ^3.0.0
dev_dependencies:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment