Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

リアルタイム通知画面のサンプル実装 #11

Merged
merged 6 commits into from
Mar 5, 2024

Conversation

Yoshida-koshi
Copy link
Collaborator

@Yoshida-koshi Yoshida-koshi commented Feb 26, 2024

概要

リアルタイム通知画面のサンプル実装を行いました!

動作確認

この間の定例会で見せた通り

参考

https://qiita.com/shohei-y/items/7880598d9c797f277731

@Yoshida-koshi Yoshida-koshi self-assigned this Feb 26, 2024
Copy link
Collaborator

@KobayashiYoh KobayashiYoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました

Comment on lines 9 to 40
class ApiService {
static const String clientId = "nItkXrOim626bBcxDxsrf-MWglvPHspWCSromG8fqDQ";
static const String clientSecret =
"LfY-blimF7OKRWiAifj-Um_CJc2PbK-Roh9irjz0QmA";
static const url =
"https://community.4nonome.com/oauth/authorize?response_type=code&client_id=$clientId&redirect_uri=https://community.4nonome.com/web/home";

Future<String> getAccessToken(Uri uri) async {
final code = uri.queryParameters["code"];
final requestBody = jsonEncode({
"grant_type": "authorization_code",
"code": code,
"client_id": clientId,
"client_secret": clientSecret,
"redirect_uri": "https://community.4nonome.com/web/home"
});
final response = await http.post(
Uri.parse("https://community.4nonome.com/oauth/token"),
headers: {
"content-type": "application/json",
},
body: requestBody,
);
if (response.statusCode == 200) {
final body = json.decode(response.body);
final accessToken = body["access_token"];
return accessToken;
} else {
throw Exception("Error");
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientIdとclientSecretが差分に入っちゃっているので、ApiServiceを削除してMastodonRepositoryクラスを呼び出してサインインする感じに修正してもらっても良いですか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました! d472ef9

Comment on lines 54 to 74
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..loadRequest(Uri.parse(ApiService.url))
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (String url) async {
final uri = Uri.parse(url);
if (uri.queryParameters['code'] != null) {
final accessToken = await ApiService().getAccessToken(uri);
if (!mounted) return;
Navigator.push(
context,
MaterialPageRoute(
builder: (_) =>
DebugRealTimeNotificationPage(accessToken: accessToken),
),
);
}
},
),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +128 to +141
channel = IOWebSocketChannel.connect(
Uri.parse(
"wss://community.4nonome.com/api/v1/streaming/?stream=user:notification&access_token=${widget.accessToken}"),
);
// リアルタイムで返ってきた通知を処理する
channel.stream.listen((data) {
Map<String, dynamic> body = json.decode(data);
Map<String, dynamic> payloadData = json.decode(body["payload"]);
if (mounted) {
setState(() {
nameList.add(payloadData['account']['username']);
});
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

天才か!?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yoくんでもできるよ笑笑

@KobayashiYoh KobayashiYoh merged commit 1073047 into develop Mar 5, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants