✨ Push notification
🧱 Added firebase
This commit is contained in:
88
lib/firebase_options.dart
Normal file
88
lib/firebase_options.dart
Normal file
@ -0,0 +1,88 @@
|
||||
// File generated by FlutterFire CLI.
|
||||
// ignore_for_file: type=lint
|
||||
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) {
|
||||
return web;
|
||||
}
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
return android;
|
||||
case TargetPlatform.iOS:
|
||||
return ios;
|
||||
case TargetPlatform.macOS:
|
||||
return macos;
|
||||
case TargetPlatform.windows:
|
||||
return windows;
|
||||
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 web = FirebaseOptions(
|
||||
apiKey: 'AIzaSyBKfIQpTouj5rXnlzkEieSlbAzepm4mgJE',
|
||||
appId: '1:961776991058:web:b91d12f2892a5609f4188b',
|
||||
messagingSenderId: '961776991058',
|
||||
projectId: 'solian-0x001',
|
||||
authDomain: 'solian-0x001.firebaseapp.com',
|
||||
storageBucket: 'solian-0x001.firebasestorage.app',
|
||||
measurementId: 'G-XY3HHKG0PE',
|
||||
);
|
||||
|
||||
static const FirebaseOptions android = FirebaseOptions(
|
||||
apiKey: 'AIzaSyDvFNudXYs29uDtcCv6pFR8h5tXBs90FYk',
|
||||
appId: '1:961776991058:android:a8d3f7995b0b8e86f4188b',
|
||||
messagingSenderId: '961776991058',
|
||||
projectId: 'solian-0x001',
|
||||
storageBucket: 'solian-0x001.firebasestorage.app',
|
||||
);
|
||||
|
||||
static const FirebaseOptions ios = FirebaseOptions(
|
||||
apiKey: 'AIzaSyCzQIyiYKoYHTpGXhN-IjgMML8z797WVD8',
|
||||
appId: '1:961776991058:ios:727229d368cc47e1f4188b',
|
||||
messagingSenderId: '961776991058',
|
||||
projectId: 'solian-0x001',
|
||||
storageBucket: 'solian-0x001.firebasestorage.app',
|
||||
iosBundleId: 'dev.solsynth.solian',
|
||||
);
|
||||
|
||||
static const FirebaseOptions macos = FirebaseOptions(
|
||||
apiKey: 'AIzaSyCzQIyiYKoYHTpGXhN-IjgMML8z797WVD8',
|
||||
appId: '1:961776991058:ios:727229d368cc47e1f4188b',
|
||||
messagingSenderId: '961776991058',
|
||||
projectId: 'solian-0x001',
|
||||
storageBucket: 'solian-0x001.firebasestorage.app',
|
||||
iosBundleId: 'dev.solsynth.solian',
|
||||
);
|
||||
|
||||
static const FirebaseOptions windows = FirebaseOptions(
|
||||
apiKey: 'AIzaSyCfgOdlcr7h8x8j0WKx_S2wXnGkOopq320',
|
||||
appId: '1:961776991058:web:3a912c0eb14028e5f4188b',
|
||||
messagingSenderId: '961776991058',
|
||||
projectId: 'solian-0x001',
|
||||
authDomain: 'solian-0x001.firebaseapp.com',
|
||||
storageBucket: 'solian-0x001.firebasestorage.app',
|
||||
measurementId: 'G-JD1YEG9D6F',
|
||||
);
|
||||
}
|
@ -1,16 +1,20 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:easy_localization/easy_localization.dart' hide TextDirection;
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:image_picker_android/image_picker_android.dart';
|
||||
import 'package:island/firebase_options.dart';
|
||||
import 'package:island/pods/config.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/theme.dart';
|
||||
import 'package:bitsdojo_window/bitsdojo_window.dart';
|
||||
import 'package:island/pods/userinfo.dart';
|
||||
import 'package:island/route.dart';
|
||||
import 'package:island/services/notify.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
|
||||
@ -18,6 +22,8 @@ import 'package:image_picker_platform_interface/image_picker_platform_interface.
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await EasyLocalization.ensureInitialized();
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
if (!kIsWeb && (Platform.isMacOS || Platform.isLinux || Platform.isWindows)) {
|
||||
@ -68,7 +74,13 @@ class IslandApp extends HookConsumerWidget {
|
||||
// Load userinfo
|
||||
final userNotifier = ref.read(userInfoProvider.notifier);
|
||||
Future(() {
|
||||
userNotifier.fetchUser();
|
||||
userNotifier.fetchUser().then((_) {
|
||||
final user = ref.watch(userInfoProvider);
|
||||
if (user.hasValue) {
|
||||
final apiClient = ref.read(apiClientProvider);
|
||||
subscribePushNotification(apiClient);
|
||||
}
|
||||
});
|
||||
});
|
||||
return null;
|
||||
}, []);
|
||||
|
@ -1,14 +1,17 @@
|
||||
import 'package:animations/animations.dart';
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:flutter_udid/flutter_udid.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:island/models/auth.dart';
|
||||
import 'package:island/pods/config.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
import 'package:island/pods/userinfo.dart';
|
||||
import 'package:island/services/notify.dart';
|
||||
import 'package:island/widgets/alert.dart';
|
||||
import 'package:island/widgets/app_scaffold.dart';
|
||||
import 'package:lucide_icons/lucide_icons.dart';
|
||||
@ -130,7 +133,10 @@ class _LoginCheckScreen extends HookConsumerWidget {
|
||||
ref.invalidate(tokenPairProvider);
|
||||
if (!context.mounted) return;
|
||||
final userNotifier = ref.read(userInfoProvider.notifier);
|
||||
userNotifier.fetchUser();
|
||||
userNotifier.fetchUser().then((_) {
|
||||
final apiClient = ref.read(apiClientProvider);
|
||||
subscribePushNotification(apiClient);
|
||||
});
|
||||
Navigator.pop(context, true);
|
||||
} catch (err) {
|
||||
showErrorAlert(err);
|
||||
@ -355,7 +361,18 @@ class _LoginLookupScreen extends HookConsumerWidget {
|
||||
final client = ref.watch(apiClientProvider);
|
||||
final resp = await client.post(
|
||||
'/auth/challenge',
|
||||
data: {'account': uname},
|
||||
data: {
|
||||
'account': uname,
|
||||
'device_id': await FlutterUdid.consistentUdid,
|
||||
'platform': switch (defaultTargetPlatform) {
|
||||
TargetPlatform.iOS => 2,
|
||||
TargetPlatform.android => 3,
|
||||
TargetPlatform.macOS => 4,
|
||||
TargetPlatform.windows => 5,
|
||||
TargetPlatform.linux => 6,
|
||||
_ => 1,
|
||||
},
|
||||
},
|
||||
);
|
||||
final result = SnAuthChallenge.fromJson(resp.data);
|
||||
onChallenge(result);
|
||||
|
54
lib/services/notify.dart
Normal file
54
lib/services/notify.dart
Normal file
@ -0,0 +1,54 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
Future<void> subscribePushNotification(Dio apiClient) async {
|
||||
await FirebaseMessaging.instance.requestPermission(
|
||||
provisional: true,
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
String? deviceToken;
|
||||
if (kIsWeb) {
|
||||
deviceToken = await FirebaseMessaging.instance.getToken(
|
||||
vapidKey:
|
||||
"BFN2mkqyeI6oi4d2PAV4pfNyG3Jy0FBEblmmPrjmP0r5lHOPrxrcqLIWhM21R_cicF-j4Xhtr1kyDyDgJYRPLgU",
|
||||
);
|
||||
} else if (Platform.isAndroid) {
|
||||
deviceToken = await FirebaseMessaging.instance.getToken();
|
||||
} else if (Platform.isIOS) {
|
||||
deviceToken = await FirebaseMessaging.instance.getAPNSToken();
|
||||
}
|
||||
|
||||
FirebaseMessaging.instance.onTokenRefresh
|
||||
.listen((fcmToken) {
|
||||
_putTokenToRemote(apiClient, fcmToken, 1);
|
||||
})
|
||||
.onError((err) {
|
||||
log("Failed to get firebase cloud messaging push token: $err");
|
||||
});
|
||||
|
||||
if (deviceToken != null) {
|
||||
_putTokenToRemote(
|
||||
apiClient,
|
||||
deviceToken,
|
||||
!kIsWeb && (Platform.isIOS || Platform.isMacOS) ? 0 : 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _putTokenToRemote(
|
||||
Dio apiClient,
|
||||
String token,
|
||||
int provider,
|
||||
) async {
|
||||
await apiClient.put(
|
||||
"/notifications/subscription",
|
||||
data: {"provider": provider, "device_token": token},
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user