-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from ia-toki/issue-55-push-notif
[Issue 55] Push Notification Manual Trigger
- Loading branch information
Showing
9 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters