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

Enhc: Added authorization support, host url, ws port and wss port can be configured dynamically in Android and iOS platform #152

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ group 'com.pusher'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.7.10'
ext.kotlin_version = '1.8.20'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -25,7 +25,12 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
// Conditional for compatibility with AGP < 4.2
if (project.android.hasProperty("namespace")) {
namespace "com.pusher.channels_flutter"
}

compileSdk 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
private val TAG = "PusherChannelsFlutter"

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
methodChannel =
MethodChannel(
flutterPluginBinding.binaryMessenger,
"pusher_channels_flutter"
)
methodChannel = MethodChannel(
flutterPluginBinding.binaryMessenger, "pusher_channels_flutter"
)
methodChannel.setMethodCallHandler(this)
}

Expand Down Expand Up @@ -66,19 +64,20 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
"connect" -> this.connect(result)
"disconnect" -> this.disconnect(result)
"subscribe" -> this.subscribe(
call.argument("channelName")!!,
result
call.argument("channelName")!!, result
)

"unsubscribe" -> this.unsubscribe(
call.argument("channelName")!!,
result
call.argument("channelName")!!, result
)

"trigger" -> this.trigger(
call.argument("channelName")!!,
call.argument("eventName")!!,
call.argument("data")!!,
result
)

"getSocketId" -> this.getSocketId(result)
else -> {
result.notImplemented()
Expand All @@ -93,15 +92,17 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
}

private fun init(
call: MethodCall,
result: Result
call: MethodCall, result: Result
) {
try {
if (pusher != null) {
pusher!!.disconnect()
}
val options = PusherOptions()
if (call.argument<String>("cluster") != null) options.setCluster(call.argument("cluster"))
if (call.argument<String>("host") != null) options.setHost(call.argument("host"))
if (call.argument<Int>("wsPort") != null) options.setWsPort(call.argument("wsPort")!!)
if (call.argument<Int>("wssPort") != null) options.setWssPort(call.argument("wssPort")!!)
if (call.argument<Boolean>("useTLS") != null) options.isUseTLS =
call.argument("useTLS")!!
if (call.argument<Long>("activityTimeout") != null) options.activityTimeout =
Expand All @@ -112,8 +113,19 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
call.argument("maxReconnectionAttempts")!!
if (call.argument<Int>("maxReconnectGapInSeconds") != null) options.maxReconnectGapInSeconds =
call.argument("maxReconnectGapInSeconds")!!
if (call.argument<String>("authEndpoint") != null) options.channelAuthorizer =
HttpChannelAuthorizer(call.argument("authEndpoint"))
if (call.argument<String>("authEndpoint") != null) {
val authorize = HttpChannelAuthorizer(call.argument("authEndpoint"))
if (call.argument<Map<String, Map<String, String>>>("authParams") != null) {
val authParams = call.argument<Map<String, Map<String, String>>>("authParams")!!
if (authParams.containsKey("headers")) {
val authHeaders = authParams["headers"]
if (authHeaders != null && authHeaders!!.isNotEmpty()) authorize.setHeaders(
authHeaders
)
}
}
options.channelAuthorizer = authorize
}
if (call.argument<String>("authorizer") != null) options.channelAuthorizer = this
if (call.argument<String>("proxy") != null) {
val (host, port) = call.argument<String>("proxy")!!.split(':')
Expand Down Expand Up @@ -142,10 +154,12 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(
channelName, this
)

channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
channelName.startsWith("presence-") -> pusher!!.subscribePresence(
channelName, this
)

else -> pusher!!.subscribe(channelName, this)
}
channel.bindGlobal(this)
Expand All @@ -162,8 +176,10 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName)
.trigger(eventName, data)

channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
.trigger(eventName, data)

else -> throw Exception("Messages can only be sent to private and presence channels.")
}
result.success(null)
Expand All @@ -180,8 +196,7 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
try {
activity!!.runOnUiThread {
methodChannel.invokeMethod("onAuthorizer", mapOf(
"channelName" to channelName,
"socketId" to socketId
"channelName" to channelName, "socketId" to socketId
), object : Result {
override fun success(o: Any?) {
if (o != null) {
Expand All @@ -192,11 +207,13 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
}
mutex.release()
}

override fun error(s: String, s1: String?, o: Any?) {
Log.i(TAG, "Pusher authorize error: " + s)
result = "{ }"
mutex.release()
}

override fun notImplemented() {
result = "{ }"
mutex.release()
Expand Down Expand Up @@ -228,7 +245,7 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
"onEvent", mapOf(
"channelName" to channelName,
"eventName" to "pusher:subscription_succeeded",
"data" to emptyMap<String,String>()
"data" to emptyMap<String, String>()
)
)
}
Expand All @@ -248,12 +265,11 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
override fun onAuthenticationFailure(message: String, e: Exception) {
callback(
"onSubscriptionError", mapOf(
"message" to message,
"error" to e.toString()
"message" to message, "error" to e.toString()
)
)
}
}

// Other ChannelEventListener methods
override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
val gson = Gson()
Expand All @@ -265,9 +281,7 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
}
val data = mapOf(
"presence" to mapOf(
"count" to users.size,
"ids" to users.map { it.id },
"hash" to hash
"count" to users.size, "ids" to users.map { it.id }, "hash" to hash
)
)
callback(
Expand All @@ -283,19 +297,16 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
override fun onDecryptionFailure(event: String?, reason: String?) {
callback(
"onDecryptionFailure", mapOf(
"event" to event,
"reason" to reason
"event" to event, "reason" to reason
)
)
}

override fun userSubscribed(channelName: String, user: User) {
callback(
"onMemberAdded", mapOf(
"channelName" to channelName,
"user" to mapOf(
"userId" to user.id,
"userInfo" to user.info
"channelName" to channelName, "user" to mapOf(
"userId" to user.id, "userInfo" to user.info
)
)
)
Expand All @@ -304,10 +315,8 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
override fun userUnsubscribed(channelName: String, user: User) {
callback(
"onMemberRemoved", mapOf(
"channelName" to channelName,
"user" to mapOf(
"userId" to user.id,
"userInfo" to user.info
"channelName" to channelName, "user" to mapOf(
"userId" to user.id, "userInfo" to user.info
)
)
)
Expand All @@ -316,9 +325,7 @@ class PusherChannelsFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAw
override fun onError(message: String, code: String?, e: Exception?) {
callback(
"onError", mapOf(
"message" to message,
"code" to code,
"error" to e.toString()
"message" to message, "code" to code, "error" to e.toString()
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {

defaultConfig {
applicationId "com.pusher.channels_flutter_example"
minSdkVersion 16
minSdkVersion flutter.minSdkVersion
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences_ios/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
NWWebSocket: 040d22f23438cc09aaeabf537beff67699c3c76d
pusher_channels_flutter: 01297e6e420e0ad61a6e5901e27bb272a6f58ec2
PusherSwift: cad631bad86cfff4b8458dce1310a7774e469b1f
Expand All @@ -40,4 +40,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: cc1f88378b4bfcf93a6ce00d2c587857c6008d3b

COCOAPODS: 1.14.3
COCOAPODS: 1.13.0
9 changes: 7 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
FDFE514985FF808219480A6A /* Pods-Runner.release.xcconfig */,
0BB366997DD8408ADB714ECE /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -156,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -217,10 +216,12 @@
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand Down Expand Up @@ -253,6 +254,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -357,6 +359,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -487,6 +490,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -509,6 +513,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
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 = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Loading