Skip to content

Commit

Permalink
fix: Fix to get the token first.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 24, 2023
1 parent 654174c commit dbdf8cf
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 101 deletions.
30 changes: 30 additions & 0 deletions packages/masamune_notification/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PODS:
- Flutter (1.0.0)
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- Flutter (from `Flutter`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.12.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -13,6 +13,7 @@
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
E503787913C8C51031A7FAB8 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FC923119050F3AA497437DC /* Pods_Runner.framework */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -29,9 +30,11 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
13864F9580ADCD28EC09B3DD /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
6FC923119050F3AA497437DC /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
Expand All @@ -42,13 +45,16 @@
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C2FE720DF125101469AC340B /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
CF1AC9EA1552ED71211EFB70 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
97C146EB1CF9000F007C117D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E503787913C8C51031A7FAB8 /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -72,6 +78,8 @@
9740EEB11CF90186004384FC /* Flutter */,
97C146F01CF9000F007C117D /* Runner */,
97C146EF1CF9000F007C117D /* Products */,
9CD9FB8D76FC7D3D26979E24 /* Pods */,
FBBF29309852D369A67DC7FD /* Frameworks */,
);
sourceTree = "<group>";
};
Expand All @@ -98,19 +106,40 @@
path = Runner;
sourceTree = "<group>";
};
9CD9FB8D76FC7D3D26979E24 /* Pods */ = {
isa = PBXGroup;
children = (
CF1AC9EA1552ED71211EFB70 /* Pods-Runner.debug.xcconfig */,
C2FE720DF125101469AC340B /* Pods-Runner.release.xcconfig */,
13864F9580ADCD28EC09B3DD /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
FBBF29309852D369A67DC7FD /* Frameworks */ = {
isa = PBXGroup;
children = (
6FC923119050F3AA497437DC /* Pods_Runner.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
97C146ED1CF9000F007C117D /* Runner */ = {
isa = PBXNativeTarget;
buildConfigurationList = 97C147051CF9000F007C117D /* Build configuration list for PBXNativeTarget "Runner" */;
buildPhases = (
49F6835790F78AB9E47ECBD0 /* [CP] Check Pods Manifest.lock */,
9740EEB61CF901F6004384FC /* Run Script */,
97C146EA1CF9000F007C117D /* Sources */,
97C146EB1CF9000F007C117D /* Frameworks */,
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
C6E3F3ED56E1090CBBFF90D9 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand All @@ -127,7 +156,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -171,10 +200,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -183,8 +214,31 @@
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" embed_and_thin";
};
49F6835790F78AB9E47ECBD0 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand All @@ -197,6 +251,23 @@
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build";
};
C6E3F3ED56E1090CBBFF90D9 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RuntimePushNotificationMasamuneAdapter
super.subscribeOnBoot = const [],
super.loggerAdapters = const [],
super.onLink,
super.onRetrievedToken,
});

static String? _token;
Expand Down
32 changes: 28 additions & 4 deletions packages/masamune_notification/lib/src/push_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
PushNotificationMasamuneAdapter get primaryAdapter =>
PushNotificationMasamuneAdapter.primary;

String? _token;
Completer<void>? _completer;
PushNotificationListenResponse? _listenResponse;

Expand Down Expand Up @@ -115,13 +116,30 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,

/// Obtain the FCM token for this terminal.
///
/// The retrieved token is retained. If you want to update the token again, set [reload] to `true`.
///
/// If the token is successfully retrieved, [onRetrievedToken] is executed. If [reload] is `false`, [onRetrievedToken] is not executed if the token has already been retrieved.
///
/// この端末のFCMトークンを取得します。
Future<String?> getToken() {
final token = adapter.getToken();
///
/// 取得されたトークンは保持されます。再度トークンを更新したい場合は[reload]`true`にしてください。
///
/// トークンの取得に成功した場合[onRetrievedToken]が実行されます。[reload]`false`の場合、トークンが既に取得されている場合は[onRetrievedToken]は実行されません。
Future<String?> getToken({
bool reload = false,
FutureOr<void> Function(String token)? onRetrievedToken,
}) async {
if (!reload && _token.isNotEmpty) {
return _token;
}
_token = await adapter.getToken();
if (_token.isNotEmpty) {
await (onRetrievedToken ?? adapter.onRetrievedToken)?.call(_token!);
}
_sendLog(PushNotificationLoggerEvent.token, parameters: {
PushNotificationLoggerEvent.tokenKey: token,
PushNotificationLoggerEvent.tokenKey: _token,
});
return token;
return _token;
}

/// Start receiving notifications.
Expand All @@ -132,15 +150,20 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
///
/// You can specify a callback when the URL is launched with [onLink].
///
/// The PUSH token is retrieved before the reception begins, but [onRetrievedToken] is executed if the token is successfully retrieved.
///
/// 通知の受け取りを開始します。
///
/// 通知を取得した場合、[value]が更新され、[notifyListeners]が呼ばれます。
///
/// [addListener]で状態を監視することで通知が来たときになにかしらの処理を行うことが可能です。
///
/// [onLink]でURLが起動されたときのコールバックを指定することができます。
///
/// 受け取りを開始する前にPUSHトークンを取得しますが、トークンの取得に成功した場合[onRetrievedToken]が実行されます。
Future<void> listen({
FutureOr<void> Function(Uri? link, bool onOpenedApp)? onLink,
FutureOr<void> Function(String token)? onRetrievedToken,
}) async {
if (_completer != null) {
return _completer?.future;
Expand All @@ -151,6 +174,7 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
_completer = Completer();
try {
_onLink = onLink;
await getToken(onRetrievedToken: onRetrievedToken);
_listenResponse = await adapter.listen(
onMessage: _onMessage,
onMessageOpenedApp: _onMessageOpenedApp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ abstract class PushNotificationMasamuneAdapter extends MasamuneAdapter {
this.subscribeOnBoot = const [],
this.loggerAdapters = const [],
this.onLink,
this.onRetrievedToken,
});

/// You can retrieve the [PushNotificationMasamuneAdapter] first given by [MasamuneAdapterScope].
Expand Down Expand Up @@ -79,6 +80,11 @@ abstract class PushNotificationMasamuneAdapter extends MasamuneAdapter {
/// URLが起動されたときのコールバック。
final FutureOr<void> Function(Uri? link, bool onOpenedApp)? onLink;

/// Callback when token is obtained.
///
/// トークンが取得された場合のコールバック。
final FutureOr<void> Function(String token)? onRetrievedToken;

@override
void onInitScope(MasamuneAdapter adapter) {
super.onInitScope(adapter);
Expand Down
2 changes: 1 addition & 1 deletion packages/masamune_notification/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ packages:
path: "../masamune_builder"
relative: true
source: path
version: "2.10.1"
version: "2.10.2"
masamune_scheduler:
dependency: "direct main"
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FirebasePushNotificationMasamuneAdapter
this.linuxOptions,
super.loggerAdapters = const [],
super.onLink,
super.onRetrievedToken,
}) : _options = options;

/// Options for initializing Firebase.
Expand Down

0 comments on commit dbdf8cf

Please sign in to comment.