Skip to content

Commit f6908a9

Browse files
committed
added support for api 12+
1 parent 1cb727a commit f6908a9

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

android/src/main/java/com/reactnativecustomtimernotification/CustomNotificationModule.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,30 @@ class CustomNotificationModule: ReactContextBaseJavaModule {
6262
intent.putExtra("id",id);
6363
intent.putExtra("action","press");
6464
intent.putExtra("payload",payload);
65-
val pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
65+
var pendingIntent:PendingIntent? = null;
6666

67+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
68+
pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
69+
} else {
70+
pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
71+
}
6772
val onCancelIntent = Intent(myContext, OnClickBroadcastReceiver::class.java)
6873
onCancelIntent.putExtra("id",id);
6974
onCancelIntent.putExtra("action","cancel");
7075
onCancelIntent.putExtra("payload",payload);
71-
val onDismissPendingIntent =
72-
PendingIntent.getBroadcast(myContext, 0, onCancelIntent, 0)
76+
var onDismissPendingIntent:PendingIntent? = null;
77+
78+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
79+
onDismissPendingIntent = PendingIntent.getBroadcast(
80+
myContext,
81+
0,
82+
onCancelIntent,
83+
PendingIntent.FLAG_MUTABLE // Set the mutability flag to mutable
84+
);
85+
} else {
86+
onDismissPendingIntent =
87+
PendingIntent.getBroadcast(myContext, 0, onCancelIntent, 0)
88+
}
7389

7490
val notificationLayout = RemoteViews(packageName, R.layout.notification_collapsed);
7591

@@ -118,6 +134,10 @@ class CustomNotificationModule: ReactContextBaseJavaModule {
118134
.setContentIntent(pendingIntent)
119135
.setDeleteIntent(onDismissPendingIntent)
120136
.setPriority(NotificationCompat.PRIORITY_HIGH);
137+
138+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
139+
notificationBuilder.setStyle(NotificationCompat.DecoratedCustomViewStyle())
140+
121141
notificationManager.notify(id,notificationBuilder.build());
122142

123143
} catch (e:Exception) {

android/src/main/java/com/reactnativecustomtimernotification/CustomTimerNotificationModule.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,32 @@ var removedNotification = false;
113113
intent.putExtra("id",id);
114114
intent.putExtra("action","press");
115115
intent.putExtra("payload",payload);
116-
val pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
116+
var pendingIntent:PendingIntent? = null;
117+
118+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
119+
pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_IMMUTABLE );
120+
} else {
121+
pendingIntent = PendingIntent.getBroadcast(myContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
122+
}
117123

118124
val onCancelIntent = Intent(myContext, OnClickBroadcastReceiver::class.java)
119125
onCancelIntent.putExtra("id",id);
120126
onCancelIntent.putExtra("action","cancel");
121127
onCancelIntent.putExtra("payload",payload);
122-
val onDismissPendingIntent =
123-
PendingIntent.getBroadcast(myContext, 0, onCancelIntent, 0)
128+
var onDismissPendingIntent:PendingIntent? = null;
129+
130+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
131+
onDismissPendingIntent = PendingIntent.getBroadcast(
132+
myContext,
133+
0,
134+
onCancelIntent,
135+
PendingIntent.FLAG_IMMUTABLE // Set the mutability flag to mutable
136+
);
137+
} else {
138+
onDismissPendingIntent =
139+
PendingIntent.getBroadcast(myContext, 0, onCancelIntent, 0)
140+
}
141+
124142

125143
val notificationLayout = RemoteViews(packageName, R.layout.notification_open);
126144
notificationLayout.setTextViewText(R.id.title,title)

android/src/main/java/com/reactnativecustomtimernotification/ForegroundService.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,31 @@ class ForegroundService : Service() {
6767
intent.putExtra("id",id);
6868
intent.putExtra("action","press");
6969
intent.putExtra("payload",payload);
70-
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
70+
var pendingIntent:PendingIntent? = null;
71+
72+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
73+
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
74+
} else {
75+
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
76+
}
7177

7278
val onCancelIntent = Intent(this, OnClickBroadcastReceiver::class.java)
7379
onCancelIntent.putExtra("id",id);
7480
onCancelIntent.putExtra("action","cancel");
7581
onCancelIntent.putExtra("payload",payload);
76-
val onDismissPendingIntent =
77-
PendingIntent.getBroadcast(this, 0, onCancelIntent, 0)
82+
var onDismissPendingIntent:PendingIntent? = null;
83+
84+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
85+
onDismissPendingIntent = PendingIntent.getBroadcast(
86+
this,
87+
0,
88+
onCancelIntent,
89+
PendingIntent.FLAG_MUTABLE // Set the mutability flag to mutable
90+
);
91+
} else {
92+
onDismissPendingIntent =
93+
PendingIntent.getBroadcast(this, 0, onCancelIntent, 0)
94+
}
7895

7996
val notificationLayout = RemoteViews(packageName, R.layout.notification_open);
8097
notificationLayout.setTextViewText(R.id.title,title)

example/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ dependencies {
182182
implementation fileTree(dir: "libs", include: ["*.jar"])
183183
//noinspection GradleDynamicVersion
184184
implementation "com.facebook.react:react-native:+" // From node_modules
185-
185+
implementation 'androidx.work:work-runtime-ktx:2.7.1'
186186

187187
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
188188
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<receiver android:name="com.reactnativecustomtimernotification.NotificationEventReceiver" />
2828
<receiver android:name="com.reactnativecustomtimernotification.OnClickBroadcastReceiver" />
2929

30-
<service android:name="com.reactnativecustomtimernotification.ForegroundService"/>
30+
<service android:name="com.reactnativecustomtimernotification.ForegroundService" android:exported="true"/>
3131

3232
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
3333
</application>

example/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
buildscript {
44
ext {
55
minSdkVersion = 16
6-
compileSdkVersion = 29
7-
targetSdkVersion = 29
6+
compileSdkVersion = 31
7+
targetSdkVersion = 31
88

99
ndkVersion = "21.1.6352462"
1010
}

0 commit comments

Comments
 (0)