Skip to content

Commit

Permalink
make changes for android 12 (#129)
Browse files Browse the repository at this point in the history
* make changes for android 12

* Fix target sdk version for sample app

* Update some dependencies to fix unit tests and warning

* bump sdk version

* Bump version
  • Loading branch information
deeborania authored Nov 17, 2021
1 parent 6799889 commit abd5a8b
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 56 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.8.0] - 2021-11-11

### Fixed
- Fix issue #126 and #127

## [1.7.0] - 2021-06-07

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
// Add these lines
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'com.pusher:push-notifications-android:1.7.0'
implementation 'com.pusher:push-notifications-android:1.8.0'
}
// Add this line to the end of the file
Expand All @@ -49,7 +49,7 @@ dependencies {
// Add these lines
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation "com.google.firebase:firebase-iid:21.1.0"
implementation 'com.pusher:push-notifications-android:1.7.0'
implementation 'com.pusher:push-notifications-android:1.8.0'
}
// Add this line to the end of the file
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
4 changes: 2 additions & 2 deletions pushnotifications-integration-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
applicationId "com.example.pushnotificationsintegrationtests"
minSdkVersion 26
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand Down
30 changes: 19 additions & 11 deletions pushnotifications/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ apply plugin: 'kotlin-android'
apply from: 'maven-push.gradle'

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.7.0"
versionName "1.8.0"

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

buildTypes {
debug{
buildConfigField "String", "VERSION_NAME", "\"${defaultConfig.versionName}\""
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "VERSION_NAME", "\"${defaultConfig.versionName}\""
}
}

Expand All @@ -43,29 +47,33 @@ dependencies {
implementation "dev.zacsweers.moshisealed:moshi-sealed-annotations:0.1.0"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.0"

compileOnly "com.google.firebase:firebase-messaging:22.0.0"
compileOnly "com.google.firebase:firebase-iid:21.1.0"

implementation("androidx.work:work-runtime-ktx:2.7.0")
implementation 'androidx.concurrent:concurrent-futures:1.1.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.annotation:annotation:1.3.0'

lintChecks project(':pushnotifications-lint')

testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test:core:1.3.0'
testImplementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'androidx.test.ext:truth:1.3.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'androidx.test.ext:junit:1.1.3'
testImplementation 'androidx.test.ext:truth:1.4.0'
testImplementation "org.mockito:mockito-core:2.13.0"

androidTestImplementation 'androidx.test:core:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.12.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

testImplementation "net.bytebuddy:byte-buddy:1.8.22"
testImplementation "net.bytebuddy:byte-buddy-agent:1.8.22"
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion pushnotifications/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.7.0
VERSION_NAME=1.8.0
VERSION_CODE=1
GROUP=com.pusher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
// not throwing a `PushNotificationsAPIBadRequest` here so that it still retries this request.
// it would make the code that calls this more complex to handle it.
// this really shouldn't happen anyway
log.e("Critical error when registering a new device (error body: ${response?.errorBody()})")
log.e("Critical error when registering a new device (error body: ${response.errorBody()})")
}

val responseBody = response?.body()
val responseBody = response.body()
if (responseBody != null && response.code() in 200..299) {
return RegisterDeviceResult(
deviceId = responseBody.id,
initialInterests = responseBody.initialInterestSet)
}

val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to register device: $error")
Expand All @@ -173,12 +173,12 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to subscribe to interest: $error")
Expand All @@ -201,12 +201,12 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to unsubscribe from interest: $error")
Expand All @@ -229,12 +229,12 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to set subscriptions: $error")
Expand All @@ -257,12 +257,12 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to refresh FCM token: $error")
Expand All @@ -289,12 +289,12 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to set device metadata: $error")
Expand All @@ -318,30 +318,30 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
throw PushNotificationsAPIDeviceNotFound()
}
if (response.code() == 400) {
val reason = response?.errorBody()?.let { safeExtractJsonError(it.string()).description }
val reason = response.errorBody()?.let { safeExtractJsonError(it.string()).description }
throw PushNotificationsAPIBadRequest(reason ?: "Unknown reason")
}
if (response.code() == 401 || response.code() == 403) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
throw PushNotificationsAPIBadJWT("${error?.error}: ${error?.description}")
throw PushNotificationsAPIBadJWT("${error.error}: ${error.description}")
}

throw PushNotificationsAPIBadJWT("Unknown reason")
}
if (response.code() == 422) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
throw PushNotificationsAPIUnprocessableEntity("${error?.error}: ${error?.description}")
throw PushNotificationsAPIUnprocessableEntity("${error.error}: ${error.description}")
}

throw PushNotificationsAPIUnprocessableEntity("Unknown reason")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to set user id: $error")
Expand Down Expand Up @@ -369,11 +369,11 @@ class PushNotificationsAPI(private val instanceId: String, overrideHostURL: Stri
// not throwing a `PushNotificationsAPIBadRequest` here so that it still retries this request.
// it would make the code that calls this more complex to handle it.
// this really shouldn't happen anyway
log.e("Critical error when deleting a device (error body: ${response?.errorBody()})")
log.e("Critical error when deleting a device (error body: ${response.errorBody()})")
}

if (response.code() !in 200..299) {
val responseErrorBody = response?.errorBody()
val responseErrorBody = response.errorBody()
if (responseErrorBody != null) {
val error = safeExtractJsonError(responseErrorBody.string())
log.w("Failed to delete device: $error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ class AppActivityLifecycleCallbacks: Application.ActivityLifecycleCallbacks {
}
}

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
}
}

class PushNotificationsInitProvider: ContentProvider() {
private val log = Logger.get(this::class)

override fun onCreate(): Boolean {
val deviceStateStore = DeviceStateStore(context)
val deviceStateStore = context?.let { DeviceStateStore(it) }

deviceStateStore.instanceIds.forEach { instanceId ->
val pni = PushNotificationsInstance(context, instanceId)
pni.onApplicationStarted()
deviceStateStore?.instanceIds?.forEach { instanceId ->
val pni = context?.let { PushNotificationsInstance(it, instanceId) }
pni?.onApplicationStarted()
}

(context.applicationContext as? Application).apply {
(context?.applicationContext as? Application).apply {
when(this) {
is Application -> registerActivityLifecycleCallbacks(AppActivityLifecycleCallbacks())
else -> log.w("Failed to register activity lifecycle callbacks. Notification delivery events might be incorrect.")
Expand All @@ -78,9 +78,9 @@ class PushNotificationsInitProvider: ContentProvider() {
return false
}

override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? = null
override fun insert(uri: Uri?, values: ContentValues?): Uri? = null
override fun update(uri: Uri?, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
override fun delete(uri: Uri?, selection: String?, selectionArgs: Array<out String>?): Int = 0
override fun getType(uri: Uri?): String? = null
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? = null
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
override fun getType(uri: Uri): String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DeviceStateStore(context: Context) {
var instanceIds: MutableSet<String>
// We need to clone the set we get from shared preferences because mutating it is not permitted
// by the Android SDK
get() = preferences.getStringSet(preferencesInstanceIdsKey, mutableSetOf<String>()).toMutableSet()
get() = preferences.getStringSet(preferencesInstanceIdsKey, mutableSetOf<String>())!!.toMutableSet()
set(value) = preferences.edit().putStringSet(preferencesInstanceIdsKey, value).apply()

fun clear() = preferences.edit().clear().commit()
Expand Down Expand Up @@ -100,7 +100,7 @@ class InstanceDeviceStateStore(context: Context, val instanceId: String?) {
var interests: MutableSet<String>
// We need to clone the set we get from shared preferences because mutating it is not permitted
// by the Android SDK
get() = preferences.getStringSet(preferencesInterestsKey, mutableSetOf<String>()).toMutableSet()
get() = preferences.getStringSet(preferencesInterestsKey, mutableSetOf<String>())!!.toMutableSet()
set(value) = preferences.edit().putStringSet(preferencesInterestsKey, value).apply()

var serverConfirmedInterestsHash: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import com.pusher.pushnotifications.reporting.api.OpenEvent
class OpenNotificationActivity: Activity() {
private val log = Logger.get(this::class)

private fun startIntent(bundle: Bundle, clickAction: String? = null) {
private fun startIntent(bundle: Bundle?, clickAction: String? = null) {
val i: Intent
if (clickAction != null) {
i = Intent()
i.action = clickAction
} else {
i = packageManager.getLaunchIntentForPackage(packageName)
i = packageManager.getLaunchIntentForPackage(packageName)!!
}

i.replaceExtras(bundle)
Expand Down
4 changes: 2 additions & 2 deletions sample_kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 31
defaultConfig {
applicationId "com.pusher.pushnotifications.sample"
minSdkVersion 26
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand Down

0 comments on commit abd5a8b

Please sign in to comment.