Skip to content

Commit

Permalink
fix: 通知権限の要求
Browse files Browse the repository at this point in the history
  • Loading branch information
YumNumm committed May 22, 2023
1 parent ae542cc commit 7284784
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 11 deletions.
8 changes: 7 additions & 1 deletion .dart_tool/package_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"packageUri": "lib/",
"languageVersion": "2.17"
},
{
"name": "app_settings",
"rootUri": "file:///Users/r_onoue/.pub-cache/hosted/pub.dev/app_settings-4.2.0",
"packageUri": "lib/",
"languageVersion": "2.12"
},
{
"name": "archive",
"rootUri": "file:///Users/r_onoue/.pub-cache/hosted/pub.dev/archive-3.3.7",
Expand Down Expand Up @@ -908,7 +914,7 @@
"languageVersion": "3.0"
}
],
"generated": "2023-05-20T11:56:02.665453Z",
"generated": "2023-05-20T15:00:13.562714Z",
"generator": "pub",
"generatorVersion": "3.0.1"
}
7 changes: 4 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
Expand All @@ -23,6 +23,7 @@ apply plugin: 'kotlin-android'

apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
namespace "net.yumnmm.eqmonitor"
compileSdk 33

compileOptions {
Expand Down Expand Up @@ -94,9 +95,9 @@ dependencies {
implementation 'com.google.firebase:firebase-perf'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.android.material:material:1.9.0-rc01'
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.work:work-runtime-ktx:2.8.1'
implementation 'androidx.core:core-splashscreen:1.0.0'
implementation 'androidx.core:core-splashscreen:1.0.1'
}

apply plugin: 'com.google.firebase.crashlytics'
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:8.0.0'
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'com.google.gms:google-services:4.3.14'

classpath 'com.google.firebase:perf-plugin:1.4.2'
Expand All @@ -31,7 +31,7 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}

Expand Down
4 changes: 2 additions & 2 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Apr 17 19:02:42 JST 2023
#Sat May 20 21:55:30 JST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PODS:
- "app_settings (3.0.0+1)":
- Flutter
- awesome_notifications (0.0.5):
- Flutter
- IosAwnCore (= 0.7.3)
Expand Down Expand Up @@ -157,6 +159,7 @@ PODS:
- FlutterMacOS

DEPENDENCIES:
- app_settings (from `.symlinks/plugins/app_settings/ios`)
- awesome_notifications (from `.symlinks/plugins/awesome_notifications/ios`)
- awesome_notifications_fcm (from `.symlinks/plugins/awesome_notifications_fcm/ios`)
- firebase_analytics (from `.symlinks/plugins/firebase_analytics/ios`)
Expand Down Expand Up @@ -188,6 +191,8 @@ SPEC REPOS:
- PromisesSwift

EXTERNAL SOURCES:
app_settings:
:path: ".symlinks/plugins/app_settings/ios"
awesome_notifications:
:path: ".symlinks/plugins/awesome_notifications/ios"
awesome_notifications_fcm:
Expand All @@ -208,6 +213,7 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
app_settings: d103828c9f5d515c4df9ee754dabd443f7cedcf3
awesome_notifications: d63d9a25f126860f9a600850d99772237895b3ba
awesome_notifications_fcm: 7e2d7ab4ca1826fe3a9a5ca96771ace73e05db48
Firebase: bd152f0f3d278c4060c5c71359db08ebcfd5a3e2
Expand Down
32 changes: 32 additions & 0 deletions lib/common/provider/app_lifecycle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Flutter imports:
import 'package:flutter/widgets.dart';

// Package imports:
import 'package:hooks_riverpod/hooks_riverpod.dart';

/// ref: https://zenn.dev/riscait/books/flutter-riverpod-practical-introduction/viewer/v2-app-lifecycle
final appLifecycleProvider = Provider<AppLifecycleState>((ref) {
final observer = _AppLifecycleObserver((value) => ref.state = value, ref);

final binding = WidgetsBinding.instance..addObserver(observer);
ref.onDispose(() => binding.removeObserver(observer));

return AppLifecycleState.resumed;
});

class _AppLifecycleObserver extends WidgetsBindingObserver {
_AppLifecycleObserver(this._didChangeState, this.ref);
final Ref ref;

final ValueChanged<AppLifecycleState> _didChangeState;

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_didChangeState(state);
super.didChangeAppLifecycleState(state);
}
}

extension AppLifecycleStateExtension on AppLifecycleState {
bool get isResumed => this == AppLifecycleState.resumed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'notification_state_model.freezed.dart';
part 'notification_state_model.g.dart';

@freezed
class NotificationStateModel with _$NotificationStateModel {
const factory NotificationStateModel({
/// 通知権限が許可されているかどうか
@Default(false) bool isAccepted,

/// 通知権限要求ダイアログを今後表示しないかどうか
@Default(false) bool neverShowNotificationPermissionDialog,

/// FCM Token
String? fcmToken,
}) = _NotificationStateModel;

factory NotificationStateModel.fromJson(Map<String, dynamic> json) =>
_$NotificationStateModelFromJson(json);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:convert';

import 'package:app_settings/app_settings.dart';
import 'package:eqmonitor/common/provider/app_lifecycle.dart';
import 'package:eqmonitor/common/provider/notification/model/notification_state_model.dart';
import 'package:eqmonitor/common/provider/shared_preferences.dart';
import 'package:flutter/widgets.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'notification_provider.g.dart';

@Riverpod(keepAlive: true)
class NotificationState extends _$NotificationState {
@override
NotificationStateModel build() {
_prefs = ref.read(sharedPreferencesProvider);
// アプリから一旦離れて戻ってきたときに
// 再度、通知権限の状態を取得する
ref.listen(appLifecycleProvider, (_, next) {
if (next == AppLifecycleState.resumed) {
init();
}
});
final res = _loadFromPrefs();
if (res != null) {}
return const NotificationStateModel();
}

late final SharedPreferences _prefs;

static const _key = 'NotificationState';

Future<void> init() async {
/// 通知権限の状態を取得する
final isNotificationPermissionAllowed = await _notificationPermission();
state = state.copyWith(
isAccepted: isNotificationPermissionAllowed,
);
}

// 通知権限のリクエスト
Future<void> requestNotificationPermission() async {
final status = await Permission.notification.request();
if (status != PermissionStatus.granted) {
// 通知設定の画面を開く
await AppSettings.openNotificationSettings();
}
await init();
}

Future<bool> _notificationPermission() async {
final status = await Permission.notification.status;
return status == PermissionStatus.granted;
}

NotificationStateModel? _loadFromPrefs() {
final data = _prefs.getString(_key);
if (data == null) {
return const NotificationStateModel();
}
return NotificationStateModel.fromJson(
jsonDecode(data) as Map<String, dynamic>,
);
}
}
3 changes: 3 additions & 0 deletions lib/feature/home/component/map/base_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';


/// 各種マップのベースマップ
/// baseMapViewModelProviderからMapStateを取得すること
class BaseMapWidget extends HookConsumerWidget {
const BaseMapWidget({super.key});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class BaseMapViewModel extends _$BaseMapViewModel {
Size? _widgetSize;
LatLng? _scaleStartedLatLng;

Animation<double>? moveAnimation = null;
Animation<double>? scaleAnimation = null;



void handleScaleStart(ScaleStartDetails details) {
if (details.pointerCount == 1) {
return;
Expand Down
9 changes: 6 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:io';

import 'package:awesome_notifications_fcm/awesome_notifications_fcm.dart';
import 'package:eqmonitor/app.dart';
import 'package:eqmonitor/common/provider/fcm_token.dart';
import 'package:eqmonitor/common/provider/shared_preferences.dart';
import 'package:eqmonitor/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
Expand All @@ -13,6 +12,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future<void> main() async {
Expand All @@ -33,24 +33,27 @@ Future<void> main() async {
String? fcmToken;
if (Platform.isAndroid || Platform.isIOS) {
fcmToken = await getFirebaseMessagingToken();

unawaited(() async {
await Permission.notification.request();
log('Firebase token: $fcmToken');
if (kDebugMode) {
unawaited(
FirebaseMessaging.instance.subscribeToTopic('config-developer'),
);
log('config-developer OK ');
}
await FirebaseMessaging.instance.subscribeToTopic('everyone');
log('everyone OK');
await FirebaseMessaging.instance.subscribeToTopic('eew');
log('eew OK');
await FirebaseMessaging.instance.subscribeToTopic('earthquake');
log('earthquake OK');
}());
}
return runApp(
ProviderScope(
overrides: [
sharedPreferencesProvider.overrideWithValue(prefs),
if (fcmToken != null) fcmTokenProvider.overrideWithValue(fcmToken),
],
child: const App(),
),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.11.2"
app_settings:
dependency: "direct main"
description:
name: app_settings
sha256: "66715a323ac36d6c8201035ba678777c0d2ea869e4d7064300d95af10c3bb8cb"
url: "https://pub.dev"
source: hosted
version: "4.2.0"
archive:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
app_settings: ^4.2.0
awesome_notifications: ^0.7.4+1
awesome_notifications_fcm: ^0.7.3
collection: ^1.17.1
Expand Down

0 comments on commit 7284784

Please sign in to comment.