Skip to content

Commit

Permalink
Fixed HMS token registration
Browse files Browse the repository at this point in the history
  • Loading branch information
intoxicated committed Sep 24, 2021
1 parent f1d00de commit a278f70
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [3.1.4] - Sep 24, 2021
* Fixed HMS push token registration
* Improved stability

## [3.1.3] - Sep 3, 2021
* Added metaData related filters for `GroupChannelListQuery` and `PublicGroupChannelListQuery`
* Fixed behavior when app life cylce state has changed
Expand Down
45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# [Sendbird](https://sendbird.com) Chat SDK for Flutter

[![Platform](https://img.shields.io/badge/platform-flutter-blue)](https://flutter.dev/)
[![Language](https://img.shields.io/badge/language-dart-blue)](https://dart.dev/)


## Table of contents

1. [Introduction](#introduction)
1. [Before getting started](#before-getting-started)
1. [Requirements](#requirements)
1. [Getting started](#getting-started)
1. [Sending your first message](#sending-your-first-message)
1. [Getting help](#getting-help)
1. [Hiring](#we-are-hiring)

<br />

Expand All @@ -17,19 +21,16 @@ Through Chat SDK for flutter, you can efficiently integrate real-time chat into

### How it works

It is simple to implement chat in your client app with Sendbird Chat SDK for Flutter: a user logs in, sees a list of channels, selects or creates an [open channel](https://sendbird.com/docs/chat/v3/flutter/guides/open-channel) or a [group channel](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel), and, through the use of the [channel event handler](https://sendbird.com/docs/chat/v3/flutter/guides/event-handler), sends messages to the channel, while also receiving them from other users within the channel.

### More about Sendbird Chat SDK for flutter
1. A user logs in
2. User sees a list of channels
3. Select or create an [open channel](https://sendbird.com/docs/chat/v3/flutter/guides/open-channel?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) or a [group channel](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk)
4. Through the use of the [channel event handler](https://sendbird.com/docs/chat/v3/flutter/guides/event-handler?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk), sends and receives messages to other users in that channel.

Find out more about Sendbird Chat for Flutter at [Sendbird Docs](https://sendbird.com/docs/chat/v3/flutter/getting-started/about-chat-sdk). If you have any comments or questions regarding bugs and feature requests, visit [Sendbird community](https://community.sendbird.com).

<br />

## Before getting started

This section shows you the prerequisites you need to check for using Sendbird Chat SDK for Flutter.

### Requirements
## Requirements

The minimum requirements for Chat SDK for Flutter are:

Expand All @@ -51,7 +52,7 @@ The fastest way to test Sendbird Chat SDK for Flutter is to build your chat app

A Sendbird application comprises everything required in a chat service including users, message, and channels. To create an application:

1. Go to the [Sendbird Dashboard](https://dashboard.sendbird.com/auth/signup) and enter your email and password, and create a new account. You can also sign up with a Google account.
1. Go to the [Sendbird Dashboard](https://dashboard.sendbird.com/auth/signup?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) and enter your email and password, and create a new account. You can also sign up with a Google account.
2. When prompted by the setup wizard, enter your organization information to manage Sendbird applications.
3. Lastly, when your dashboard home appears after completing setup, click **Create +** at the top-right corner.

Expand Down Expand Up @@ -116,9 +117,9 @@ try {

#### B. Using a unique user ID and an access token

By using Chat Platform API, you can [create a user](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user) and issue a unique access token to each user, or [issue an access token](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-update-a-user) for an existing user. Once an access token is issued, a user is required to provide the access token to log in to your Sendbird application.
By using Chat Platform API, you can [create a user](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user) and issue a unique access token to each user, or [issue an access token](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-update-a-user?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) for an existing user. Once an access token is issued, a user is required to provide the access token to log in to your Sendbird application.

1. Using the [Chat Platform API](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user), create a Sendbird user account with the information submitted when a user signs up or signs in to your service.
1. Using the [Chat Platform API](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk), create a Sendbird user account with the information submitted when a user signs up or signs in to your service.
2. Save the user ID along with the issued access token to your securely managed persistent storage.
3. When a user attempts to log in to the application, load the user ID and access token from the storage, and then pass them to `connect()` method.
4. Periodically replacing the user's access token is recommended for account security.
Expand All @@ -135,11 +136,11 @@ try {

To manage user access to your Sendbird application, go to **Settings** > **Application** > **Security** > **Access token permission** setting on your Sendbird dashboard. You can change settings to prevent the users without an access token from logging in to your application or restrict their access to read and write messages.

For security reasons, you can also use a session token when a user logs in to Sendbird server instead of an access token. Learn more about [Access token vs. Session token](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user-3-access-token-vs-session-token) from the Chat Platform API guide.
For security reasons, you can also use a session token when a user logs in to Sendbird server instead of an access token. Learn more about [Access token vs. Session token](https://sendbird.com/docs/chat/v3/platform-api/guides/user#2-create-a-user-3-access-token-vs-session-token?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) from the Chat Platform API guide.

### Step 3: Create a new open channel

Create an [open channel](https://sendbird.com/docs/chat/v3/flutter/guides/open-channel#2-create-a-channel). Once created, all users in your Sendbird application can easily participate in the channel.
Create an [open channel](https://sendbird.com/docs/chat/v3/flutter/guides/open-channel#2-create-a-channel?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk). Once created, all users in your Sendbird application can easily participate in the channel.

```dart
try {
Expand All @@ -149,7 +150,7 @@ try {
}
```

You can also create a [group channel](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel#2-create-a-channel) by [inviting users as new members](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel#2-invite-users-as-members) to the channel.
You can also create a [group channel](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel#2-create-a-channel?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) by [inviting users as new members](https://sendbird.com/docs/chat/v3/flutter/guides/group-channel#2-invite-users-as-members?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) to the channel.

> Note: The majority of the methods used in the following steps are all asynchronous. This means, with asynchronous methods, your client app must receive a result via `await` or `then()` callbacks from Sendbird server through completion handlers before moving on to the next step.

Expand All @@ -168,11 +169,12 @@ try {

### Step 5: Send a message to the channel

Finally, send a message to the channel. There are three types of [messages](https://sendbird.com/docs/chat/v3/platform-api/guides/messages#-3-resource-representation): a user message in a plain text, a file message in a binary file, such as an image or PDF, and an admin message in a plain text sent through the [dashboard](https://dashboard.sendbird.com/auth/signin) or [Chat Platform API](https://sendbird.com/docs/chat/v3/platform-api/guides/messages#2-send-a-message).
Finally, send a message to the channel. There are three types of [messages](https://sendbird.com/docs/chat/v3/platform-api/guides/messages#-3-resource-representation?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk): a user message in a plain text, a file message in a binary file, such as an image or PDF, and an admin message in a plain text sent through the [dashboard](https://dashboard.sendbird.com/auth/signin?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) or [Chat Platform API](https://sendbird.com/docs/chat/v3/platform-api/guides/messages#2-send-a-message?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk).

```dart
try {
final params = UserMessageParams(message: MESSAGE)
final params = UserMessageParams()
..message = MESSAGE
..data = DATA
..customType = CUSTOM_TYPE;
Expand All @@ -188,3 +190,12 @@ try {
// error
}
```


## Getting Help
Check out the Official Sendbird [Flutter docs](https://sendbird.com/docs/chat/v3/flutter/quickstart/send-first-message?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) and Sendbird's [Developer Portal](https://sendbird.com/developer?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk) for tutorials and videos. If you need any help in resolving any issues or have questions, visit our [community forums](https://community.sendbird.com?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk).

<br />

## We are Hiring!
Sendbird is made up of a diverse group of humble, friendly, and hardworking individuals united by a shared purpose to build the next generation of mobile & social technologies. Join our team remotely or at one of our locations in San Mateo, Seoul, New York, London, and Singapore. More information on a [careers page](https://sendbird.com/careers?&utm_source=github&utm_medium=referral&utm_campaign=repo&utm_content=sendbird-chat-flutter-sdk).
13 changes: 13 additions & 0 deletions lib/constant/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,19 @@ enum PushTokenRegistrationStatus { success, pending, error }
/// Represents push token type
enum PushTokenType { none, hms, fcm, apns }

String? pushTokenToString(PushTokenType type) {
switch (type) {
case PushTokenType.apns:
return 'apns';
case PushTokenType.fcm:
return 'gcm';
case PushTokenType.hms:
return 'huawei';
default:
return null;
}
}

/// Represents unread item key filter
enum UnreadItemKey {
@JsonValue('non_super_group_channel_unread_message_count')
Expand Down
2 changes: 0 additions & 2 deletions lib/events/login_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class LoginEvent extends SessionEvent {

LoginEvent({
String? ekey,
int? expiresIn,
String? newKey,
String? key,
required this.loginTimestamp,
Expand All @@ -43,7 +42,6 @@ class LoginEvent extends SessionEvent {
ekey: ekey,
newKey: newKey,
key: key,
expiresIn: expiresIn,
);

factory LoginEvent.fromJson(Map<String, dynamic> json) {
Expand Down
1 change: 0 additions & 1 deletion lib/events/login_event.g.dart

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

4 changes: 2 additions & 2 deletions lib/events/session_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ part 'session_event.g.dart';
@JsonSerializable(createToJson: false)
class SessionEvent extends BaseEvent {
@JsonKey(defaultValue: 0)
final int? expiresIn;
final int? expiresAt;

final String? newKey;

Expand All @@ -17,7 +17,7 @@ class SessionEvent extends BaseEvent {

String get sessionKey => key ?? newKey ?? '';

SessionEvent({this.expiresIn, this.newKey, this.ekey, this.key});
SessionEvent({this.expiresAt, this.newKey, this.ekey, this.key});

factory SessionEvent.fromJson(Map<String, dynamic> json) =>
_$SessionEventFromJson(json);
Expand Down
2 changes: 1 addition & 1 deletion lib/events/session_event.g.dart

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

5 changes: 2 additions & 3 deletions lib/managers/command_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ class CommandManager with SdkAccessor {
sdk.sessionManager
..setUserId(user.userId)
..setEKey(event.ekey)
..setSessionKey(event.sessionKey)
..setSessionExpiresIn(event.expiresIn);
..setSessionKey(event.sessionKey);

if (wasReconnecting) {
eventManager.notifyReconnectionSucceeded();
Expand Down Expand Up @@ -528,7 +527,7 @@ class CommandManager with SdkAccessor {
Future<void> _processSessionRefresh(Command cmd) async {
final event = SessionEvent.fromJson(cmd.payload);
sdk.sessionManager.setSessionKey(event.sessionKey);
sdk.sessionManager.setSessionExpiresIn(event.expiresIn);
sdk.sessionManager.setSessionExpiresAt(event.expiresAt);
}

Future<void> _processError(Command cmd) async {
Expand Down
18 changes: 8 additions & 10 deletions lib/managers/session_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SessionManager with SdkAccessor {
String? _accessToken;
late String _sessionKeyPath;
late String _userIdKeyPath;
int _sessionExpiresIn = 0;
int _sessionExpiresAt = 0;

bool isRefreshingKey = false;

Expand Down Expand Up @@ -52,11 +52,11 @@ class SessionManager with SdkAccessor {
_userIdKeyPath = path;
}

void setSessionExpiresIn(int? timestamp) {
_sessionExpiresIn = timestamp ?? 0;
void setSessionExpiresAt(int? timestamp) {
_sessionExpiresAt = timestamp ?? 0;
}

int get sessionExpiresIn => _sessionExpiresIn;
int get sessionExpiresAt => _sessionExpiresAt;

/// Set a `sessionKey` that will be used for SDK globally
///
Expand Down Expand Up @@ -202,14 +202,12 @@ class SessionManager with SdkAccessor {
setSessionKey(payload['new_key']);
}

final reconnect = sessionExpiresIn <= 0;

if (payload['expires_in'] != null) {
setSessionExpiresIn(payload['expires_in']);
if (payload['expires_at'] != null) {
setSessionExpiresAt(payload['expires_at']);
}

eventManager.notifySessionRefreshed();
if (reconnect) sdk.reconnect(reset: true);
sdk.reconnect(reset: true);
}

// Handler for refresh session success case
Expand All @@ -231,7 +229,7 @@ class SessionManager with SdkAccessor {

// Resets session manager
void cleanUp() {
_sessionExpiresIn = 0;
_sessionExpiresAt = 0;
_eKey = null;
_sessionKey = null;
}
Expand Down
7 changes: 2 additions & 5 deletions lib/params/file_message_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ class FileMessageParams extends BaseMessageParams {
),
super.withMessage(fileMessage, deepCopy: deepCopy);

FileMessageParams.withFile(
File file, {
String? name,
FileMessage? fileMessage,
}) : uploadFile = FileInfo.fromData(
FileMessageParams.withFile(File file, {String? name})
: uploadFile = FileInfo.fromData(
name: name ?? 'my_file',
file: file,
mimeType: lookupMimeType(file.path),
Expand Down
6 changes: 5 additions & 1 deletion lib/request/user/push/push_register_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class UserPushTokenRegisterRequest extends ApiRequest {
bool unique = false,
String? userId,
}) : super(userId: userId) {
final typeUrl = type == PushTokenType.fcm ? 'gcm' : type.asString();
final typeUrl = pushTokenToString(type);
if (typeUrl == null) {
return;
}

url = 'users/${userId ?? state.userId}/push/$typeUrl';

body = {
Expand Down
2 changes: 1 addition & 1 deletion lib/sdk/internal/sendbird_sdk_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'package:sendbird_sdk/utils/async/async_queue.dart';
import 'package:sendbird_sdk/utils/logger.dart';
import 'package:sendbird_sdk/utils/parsers.dart';

const sdk_version = '3.1.3';
const sdk_version = '3.1.4';
const platform = 'flutter';

/// Internal implementation for main class. Do not directly access this class.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sendbird_sdk
description: The Flutter SDK for Sendbird Chat brings modern messenger chat features to your iOS and Android deployments
version: 3.1.3
version: 3.1.4
homepage: https://www.sendbird.com
repository: https://www.github.com/sendbird/sendbird-sdk-flutter
documentation: https://sendbird.com/docs/chat/v3/flutter/getting-started/about-chat-sdk
Expand Down

0 comments on commit a278f70

Please sign in to comment.