-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
…time_notification
…time_notification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントしました
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"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clientIdとclientSecretが差分に入っちゃっているので、ApiServiceを削除してMastodonRepositoryクラスを呼び出してサインインする感じに修正してもらっても良いですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正しました! d472ef9
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), | ||
), | ||
); | ||
} | ||
}, | ||
), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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']); | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
天才か!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yoくんでもできるよ笑笑
概要
リアルタイム通知画面のサンプル実装を行いました!
動作確認
この間の定例会で見せた通り
参考
https://qiita.com/shohei-y/items/7880598d9c797f277731