Skip to content

Commit

Permalink
Merge pull request #72 from ia-toki/issue-55-push-notif
Browse files Browse the repository at this point in the history
[Issue 55] Push Notification Manual Trigger
  • Loading branch information
12henbx authored Nov 22, 2023
2 parents 82c2a57 + fc7fe85 commit c0ca9be
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<application
android:label="bebras_pandai"
android:label="Bebras Pandai"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true">
Expand Down Expand Up @@ -34,5 +34,8 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/notification_icon" />
</application>
</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions app/lib/api/firebase_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

import '../services/firebase_service.dart';
import '../services/router_service.dart';

class FirebaseApi {
final _firebaseMessaging = FirebaseMessaging.instance;
final currentUser = FirebaseService.auth().currentUser;

void handleMessage(RemoteMessage? message) {
if (message == null) return;

router.push('/main');
}

Future<dynamic> initPushNotfications() async {
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);

await FirebaseMessaging.instance.getInitialMessage().then(handleMessage);
FirebaseMessaging.onMessageOpenedApp.listen(handleMessage);
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
}

Future<void> initNotifications() async {
await _firebaseMessaging.requestPermission();

FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
Map<String, dynamic> deviceToken = {
'fcm_token': fcmToken,
'fcm_timestamp': FieldValue.serverTimestamp(),
};

// Reference to the Firestore collection
CollectionReference<Map<String, dynamic>> fcmTokensCollection =
FirebaseFirestore.instance.collection('registered_user');

// Set the device token in Firestore
fcmTokensCollection.doc(currentUser!.uid).update(deviceToken);
}).onError((err) {
// Error getting token.
});

final fCMToken = await _firebaseMessaging.getToken();
print('Token: $fCMToken');
await initPushNotfications();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ class _RegisterPageState extends State<RegisterPage> {
(val) {
return state.province.error;
},
state.province.value,
state.province.value.isNotEmpty
? state.province.value
: 'Provinsi',
),
BiroBebrasDropdown(
'Bebras Biro',
Expand All @@ -176,7 +178,9 @@ class _RegisterPageState extends State<RegisterPage> {
(val) {
return state.bebrasBiro.error;
},
state.bebrasBiro.value,
state.bebrasBiro.value.isNotEmpty
? state.bebrasBiro.value
: 'Bebras Biro',
),
const SizedBox(height: 20),
BlocConsumer<UserRegisterBloc, RegisterFormState>(
Expand Down
2 changes: 2 additions & 0 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

import 'api/firebase_api.dart';
import 'app.dart';
import 'services/di.dart';
import 'services/firebase_service.dart';
Expand All @@ -10,5 +11,6 @@ Future<void> main() async {
await dotenv.load();
configureDependencies();
await FirebaseService.initialize();
await FirebaseApi().initNotifications();
runApp(const App());
}
6 changes: 6 additions & 0 deletions app/lib/services/firebase_service.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

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

Future<void> handleBackgroundMessage(RemoteMessage message) async {
print('Title: ${message.notification?.title}');
}

class FirebaseService {
static late FirebaseApp app;

static Future<void> initialize() async {
app = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
Expand Down
2 changes: 2 additions & 0 deletions app/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Foundation
import cloud_firestore
import firebase_auth
import firebase_core
import firebase_messaging
import firebase_storage
import flutter_local_notifications
import geolocator_apple
Expand All @@ -20,6 +21,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
FLTFirebaseStoragePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseStoragePlugin"))
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
Expand Down
32 changes: 28 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ packages:
dependency: transitive
description:
name: _flutterfire_internals
sha256: "7bcb5c5d62b3907fb4a269c0f0843df46760d38e12829a715f2ff1fb492f19ef"
sha256: "5dadadeecceac19d6a63c9d2e037bb8df58ddd4aedb94e8a056af2f39ee50f9d"
url: "https://pub.dev"
source: hosted
version: "1.3.10"
version: "1.3.11"
analyzer:
dependency: transitive
description:
Expand Down Expand Up @@ -357,10 +357,10 @@ packages:
dependency: "direct main"
description:
name: firebase_core
sha256: "37299e4907391d7fac8c7ea059bb3292768cc07b72b6c6c777675cc58da2ef4d"
sha256: "7706f4ade6cc2698c70074083bc262586a185047f6bfdd53938dcc35d35cbb9e"
url: "https://pub.dev"
source: hosted
version: "2.20.0"
version: "2.21.0"
firebase_core_platform_interface:
dependency: transitive
description:
Expand All @@ -377,6 +377,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.8.1"
firebase_messaging:
dependency: "direct main"
description:
name: firebase_messaging
sha256: "53952a6f7860c44429bec80719c411e0ff77ce6cf31fade1515c7bdd87abe4a1"
url: "https://pub.dev"
source: hosted
version: "14.7.3"
firebase_messaging_platform_interface:
dependency: transitive
description:
name: firebase_messaging_platform_interface
sha256: "543390d1c76aaf3fa563de223d1732a5def5a5efe31428e43a44e9a47efc5ed3"
url: "https://pub.dev"
source: hosted
version: "4.5.12"
firebase_messaging_web:
dependency: transitive
description:
name: firebase_messaging_web
sha256: ecfe4e851652dc5f40f1e42efcac0c2bc6bf5ef08faca936c94e1472c247d222
url: "https://pub.dev"
source: hosted
version: "3.5.12"
firebase_storage:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies:
firebase_storage: ^11.3.1
flutter_html: ^3.0.0-beta.2
printing: ^5.11.0
firebase_messaging: ^14.7.3

dev_dependencies:
build_runner: null
Expand Down

0 comments on commit c0ca9be

Please sign in to comment.