Skip to content

Commit d0d4a9f

Browse files
Merge pull request #192 from Countly/sdk34-broadcast-receiver-fix
Fixed crash for SDK34 BroadcastReceivers
2 parents 9c37f53 + a107868 commit d0d4a9f

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Added a call to enroll users to A/B tests when getting all remote config values: 'getAllValuesAndEnroll'
55
* Adding app version to every request
66

7+
* Fixed crash for SDK34 BroadcastReceivers, now declared broadcast receiver not exported if API is 34 and up.
8+
79
* Mitigated an issue where users could not enroll to an A/B tests if enrollment request has failed
810
* Mitigated an issue where users could not exit from A/B tests if removal request has failed
911

app/build.gradle

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ repositories {
2424

2525
//required for huawei push
2626
maven {
27-
url "https://developer.huawei.com/repo/"
27+
url "https://developer.huawei.com/repo/"
2828
}
2929
}
3030

3131
android {
32-
compileSdkVersion 33
33-
buildToolsVersion '33.0.0'
32+
compileSdk 34
33+
namespace 'ly.count.android.demo'
3434

3535
signingConfigs {
3636
release {
@@ -45,8 +45,8 @@ android {
4545

4646
defaultConfig {
4747
applicationId "ly.count.android.demo"
48-
minSdkVersion 17
49-
targetSdkVersion 33
48+
minSdk 17
49+
targetSdk 34
5050
versionCode 1
5151
versionName "1.0"
5252
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -91,13 +91,14 @@ dependencies {
9191
//implementation 'ly.count.android:sdk:21.11.0'
9292
//implementation 'ly.count.android:sdk-plugin:21.11.0'
9393

94+
9495
//required for FCM push
9596
implementation 'com.google.firebase:firebase-messaging:22.0.0'
9697
//required for huawei push
9798
implementation 'com.huawei.hms:push:6.3.0.304'
9899

99100
implementation 'com.google.android.material:material:1.6.0-alpha02'
100-
implementation 'androidx.appcompat:appcompat:1.4.1'
101+
implementation 'androidx.appcompat:appcompat:1.6.1'
101102
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
102103
implementation 'androidx.annotation:annotation:1.3.0'
103104
implementation 'androidx.core:core-ktx:1.7.0'

app/src/main/java/ly/count/android/demo/App.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ public void onReceive(Context context, Intent intent) {
293293
};
294294
IntentFilter filter = new IntentFilter();
295295
filter.addAction(CountlyPush.SECURE_NOTIFICATION_BROADCAST);
296-
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
296+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
297+
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null, Context.RECEIVER_NOT_EXPORTED);
298+
}
299+
else {
300+
registerReceiver(messageReceiver, filter, getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
301+
}
297302
}
298303
}

sdk/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ buildscript {
1212
}
1313

1414
android {
15-
compileSdkVersion 33
16-
buildToolsVersion "33.0.0"
15+
compileSdk 34
16+
buildToolsVersion "34.0.0"
1717

1818
defaultConfig {
19-
minSdkVersion 17
20-
targetSdkVersion 33
19+
minSdk 17
20+
targetSdk 34
2121

2222
testInstrumentationRunner 'ly.count.android.sdk.test.InstrumentationTestRunner'
2323
testHandleProfiling true

sdk/src/main/java/ly/count/android/sdk/DeviceInfo.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,13 @@ public String getDiskTotal() {
437437
@Override
438438
public String getBatteryLevel(Context context) {
439439
try {
440-
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
440+
Intent batteryIntent;
441+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
442+
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED), null, null, Context.RECEIVER_NOT_EXPORTED);
443+
}
444+
else {
445+
batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
446+
}
441447
if (batteryIntent != null) {
442448
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
443449
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

sdk/src/main/java/ly/count/android/sdk/messaging/CountlyPush.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,12 @@ public void onActivityDestroyed(Activity activity) {
784784
IntentFilter filter = new IntentFilter();
785785
filter.addAction(Countly.CONSENT_BROADCAST);
786786
BroadcastReceiver consentReceiver = new ConsentBroadcastReceiver();
787-
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
787+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
788+
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null, Context.RECEIVER_NOT_EXPORTED);
789+
}
790+
else {
791+
countlyConfigPush.application.registerReceiver(consentReceiver, filter, countlyConfigPush.application.getPackageName() + COUNTLY_BROADCAST_PERMISSION_POSTFIX, null);
792+
}
788793
}
789794

790795
if (countlyConfigPush.provider == Countly.CountlyMessagingProvider.HMS && getPushConsent(countlyConfigPush.application)) {

0 commit comments

Comments
 (0)