Skip to content

Commit

Permalink
Fix errors with specific Android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Jan 5, 2024
1 parent f86e242 commit 8c8c326
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application>
<receiver android:name=".AlarmReceiver" />
<service android:name=".AlarmService" />
<service
android:name=".alarm.AlarmService"
android:exported="false"
android:foregroundServiceType="mediaPlayback">
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ class AlarmPlugin: FlutterPlugin, MethodCallHandler {
)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class AlarmService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null) {
stopSelf()
return START_NOT_STICKY
}

val action = intent?.action
val id = intent?.getIntExtra("id", 0) ?: 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ class NotificationHandler(private val context: Context) {
fun buildNotification(title: String, body: String, fullScreen: Boolean, pendingIntent: PendingIntent): Notification {
val iconResId = context.resources.getIdentifier("ic_launcher", "mipmap", context.packageName)
val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
val pendingIntent = PendingIntent.getActivity(
val notificationPendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val notificationBuilder = Notification.Builder(context, CHANNEL_ID)
val notificationBuilder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(context, CHANNEL_ID) // For API 26 and above
} else {
Notification.Builder(context) // For lower API levels
}

notificationBuilder
.setSmallIcon(iconResId)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendingIntent)
.setContentIntent(notificationPendingIntent)
.setSound(null)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

Expand All @@ -63,7 +69,7 @@ class NotificationHandler(private val context: Context) {
}

if (fullScreen) {
notificationBuilder.setFullScreenIntent(pendingIntent, true)
notificationBuilder.setFullScreenIntent(notificationPendingIntent, true)
}

return notificationBuilder.build()
Expand Down

0 comments on commit 8c8c326

Please sign in to comment.