@@ -9,29 +9,32 @@ import android.content.BroadcastReceiver
9
9
import android.content.Context
10
10
import android.content.Intent
11
11
import android.content.IntentFilter
12
- import android.content.pm.PackageManager
13
12
import android.graphics.Color
13
+ import android.icu.text.SimpleDateFormat
14
+ import android.icu.util.Calendar
14
15
import android.os.Build
15
16
import android.os.CountDownTimer
17
+ import android.os.Handler
18
+ import android.os.SystemClock
16
19
import android.util.Log
17
20
import android.view.View
18
21
import android.widget.RemoteViews
19
22
import androidx.core.app.NotificationCompat
20
- import com.facebook.react.bridge.Callback
23
+ import com.facebook.react.bridge.Arguments
21
24
import com.facebook.react.bridge.ReactApplicationContext
22
25
import com.facebook.react.bridge.ReactContextBaseJavaModule
23
26
import com.facebook.react.bridge.ReactMethod
24
27
import com.facebook.react.bridge.ReadableMap
28
+ import com.facebook.react.bridge.WritableMap
25
29
import com.facebook.react.modules.core.DeviceEventManagerModule
26
- import org.json.JSONObject
27
- import com.facebook.react.bridge.WritableMap;
28
- import com.facebook.react.bridge.Arguments;
30
+ import java.util.*
31
+ import kotlin.time.milliseconds
29
32
30
33
31
34
class CustomTimerNotificationModule : ReactContextBaseJavaModule {
32
35
var loading : Boolean = false ;
33
- var firstForegound : Boolean = true ;
34
- var ifCancel = false ;
36
+ var foregound : Boolean = false ;
37
+
35
38
lateinit var notificationManager: NotificationManager
36
39
lateinit var builder: Notification .Builder
37
40
val channelId: String = " 255"
@@ -47,13 +50,13 @@ class CustomTimerNotificationModule: ReactContextBaseJavaModule {
47
50
myContext.registerReceiver(object : BroadcastReceiver () {
48
51
override fun onReceive (context : Context , intent : Intent ) {
49
52
try {
50
-
51
- ifCancel = true
52
53
val extras = intent.extras
53
54
val params: WritableMap = Arguments .createMap()
54
55
params.putInt(" id" , extras!! .getInt(" id" ))
55
56
params.putString(" action" , extras!! .getString(" action" ))
56
57
params.putString(" payload" , extras!! .getString(" payload" ))
58
+ removeNotification(extras!! .getInt(" id" ),foregound)
59
+
57
60
sendEvent(" notificationClick" , params)
58
61
} catch (e: Exception ) {
59
62
println (e)
@@ -87,6 +90,17 @@ class CustomTimerNotificationModule: ReactContextBaseJavaModule {
87
90
val payload = objectData.getString(" payload" );
88
91
val id = objectData.getInt(" id" );
89
92
93
+ val datetime = objectData.getString(" date" )
94
+ val sdf = SimpleDateFormat (" dd-MM-yyyy HH:mm:ss" , Locale .ENGLISH )
95
+
96
+ val startTime = SystemClock .elapsedRealtime()
97
+ val endTime: Calendar = Calendar .getInstance()
98
+ endTime.time = sdf.parse(datetime)
99
+
100
+ val now = Date ()
101
+ val elapsed: Long = now.getTime() - endTime.timeInMillis
102
+ val remainingTime = startTime - elapsed
103
+
90
104
val intent = Intent (myContext, NotificationEventReceiver ::class .java)
91
105
intent.flags = Intent .FLAG_ACTIVITY_CLEAR_TOP or Intent .FLAG_ACTIVITY_NEW_TASK
92
106
intent.putExtra(" id" ,id);
@@ -104,17 +118,16 @@ class CustomTimerNotificationModule: ReactContextBaseJavaModule {
104
118
val notificationLayout = RemoteViews (packageName, R .layout.notification_open);
105
119
notificationLayout.setTextViewText(R .id.title,title)
106
120
notificationLayout.setTextViewText(R .id.text,body)
107
- notificationLayout.setTextViewText(R .id.timer,remainingTime)
121
+
122
+ // notificationLayout.setTextViewText(R.id.timer,remainingTime)
123
+ notificationLayout.setChronometerCountDown(R .id.simpleChronometer, true );
124
+ notificationLayout.setChronometer(R .id.simpleChronometer, remainingTime, (" %sM:%sS" ), true );
125
+
108
126
109
127
// try {
110
128
// notificationLayout.setTextColor(R.id.timer , Color.parseColor(objectData.getString("timeColor")));
111
129
// } catch (e:Exception){}
112
130
113
- if (visbleTimer){
114
- notificationLayout.setViewVisibility (R .id.timer,
115
- View .INVISIBLE )
116
- }
117
-
118
131
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
119
132
val notificationChannel =
120
133
NotificationChannel (channelId, " Timer" , NotificationManager .IMPORTANCE_HIGH )
@@ -127,7 +140,6 @@ class CustomTimerNotificationModule: ReactContextBaseJavaModule {
127
140
val notificationBuilder: NotificationCompat .Builder =
128
141
NotificationCompat .Builder (myContext,channelId)
129
142
notificationBuilder.setAutoCancel(true )
130
- .setWhen(System .currentTimeMillis())
131
143
.setSmallIcon(myContext.getResources().getIdentifier(" ic_launcher" , " mipmap" , myContext.getPackageName()))
132
144
.setContentTitle(title)
133
145
.setContentText(body)
@@ -137,70 +149,54 @@ class CustomTimerNotificationModule: ReactContextBaseJavaModule {
137
149
.setContentIntent(pendingIntent)
138
150
.setDeleteIntent(onDismissPendingIntent)
139
151
.setPriority(NotificationCompat .PRIORITY_HIGH )
140
- return notificationBuilder
152
+ .setWhen(endTime.getTimeInMillis());
153
+ val handler = Handler ()
154
+ handler.postDelayed({
155
+ notificationLayout.setChronometer(R .id.simpleChronometer, remainingTime, (" %sM:%sS" ), false );
156
+ try {
157
+ val remove = objectData.getBoolean(" remove" );
158
+ val foreground = objectData.getBoolean(" foreground" );
159
+ if (remove){
160
+ removeNotification(id,foreground)
161
+ } else {
162
+ notificationLayout.setViewVisibility (R .id.simpleChronometer,
163
+ View .INVISIBLE )
164
+ }
165
+ } catch (e: Exception ){}
141
166
167
+ notificationBuilder.setCustomContentView(notificationLayout)
168
+ notificationManager.notify(id,notificationBuilder.build())
169
+ }, Math .abs(elapsed))
170
+ return notificationBuilder
142
171
}
172
+
143
173
fun updatePop (objectData : ReadableMap ,remainingTime : String ,visbleTimer : Boolean ){
144
174
val id = objectData.getInt(" id" );
145
175
val notificationBuilder: NotificationCompat .Builder = notificationPop(objectData,remainingTime,visbleTimer)
146
176
notificationManager.notify(id,notificationBuilder.build())
147
177
}
148
178
fun removeNotification (id : Int ,foreground : Boolean ) {
149
-
150
179
val notificationManager = myContext.getSystemService(NotificationManager ::class .java)
151
180
if (foreground)
152
181
ForegroundService .stopService(myContext)
153
182
else
154
183
notificationManager.cancel( id ) ;
155
184
}
156
- fun countdown (objectData : ReadableMap ,sec : Int ) {
157
- val secLong = sec.toLong()* 1000 ;
158
- val id = objectData.getInt(" id" );
159
- val foreground = objectData.getBoolean(" foreground" );
160
-
161
- object : CountDownTimer (secLong, 1000 ) {
162
- override fun onTick (millisUntilFinished : Long ) {
163
- val remainingSec: Int = (millisUntilFinished / 1000 ).toInt()
164
- val min = convert((remainingSec/ 60 ).toInt().toString())
165
- val secInMin = convert((remainingSec% 60 ).toInt().toString())
166
- val remainingTime: String = " $min : $secInMin "
167
- if (ifCancel){
168
- cancel();
169
- if (foreground){
170
- ForegroundService .stopService(myContext)
171
- }
172
- }
173
- else {
174
- if (foreground&& firstForegound){
175
- firstForegound= false
176
- ForegroundService .startService(myContext,objectData,remainingTime)
177
- } else
178
- updatePop(objectData,remainingTime,false )
185
+ @ReactMethod
186
+ fun TimerNotification (objectData : ReadableMap ) {
179
187
180
- }
181
- }
188
+ val remainingSec: Int = (10000 / 1000 ).toInt()
189
+ val min = convert((remainingSec/ 60 ).toInt().toString())
190
+ val secInMin = convert((remainingSec% 60 ).toInt().toString())
191
+ val remainingTime: String = " $min : $secInMin "
182
192
183
- override fun onFinish () {
184
- try {
185
- val remove = objectData.getBoolean(" remove" );
186
- if (remove){
187
- removeNotification(id,foreground)
188
- } else {
189
- println (" onFinish" )
190
- updatePop(objectData," " ,true )
191
- }
192
- } catch (e: Exception ){}
193
+ val foreground = objectData.getBoolean(" foreground" );
193
194
194
- }
195
- }.start()
196
- }
197
-
198
- @ReactMethod
199
- fun TimerNotification (objectData : ReadableMap ) {
200
- firstForegound= true ;
201
- ifCancel = false ;
202
- val sec = objectData.getInt(" sec" );
203
- countdown(objectData,sec)
195
+ if (foreground){
196
+ foregound= true ;
197
+ ForegroundService .startService(myContext,objectData)
198
+ } else
199
+ updatePop(objectData,remainingTime,false )
204
200
// promise.resolve(a * b)
205
201
206
202
}
0 commit comments