Skip to content

Commit

Permalink
feat(android_notif): Add daily notification in android platform and s…
Browse files Browse the repository at this point in the history
…et the option in profile view (#1380)
  • Loading branch information
NyraSama authored Apr 9, 2024
1 parent ad7433b commit 5b19c42
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 10 deletions.
2 changes: 1 addition & 1 deletion yaki_mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ android {
applicationId "com.xpeho.yaki"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 26
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
6 changes: 5 additions & 1 deletion yaki_mobile/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

<application
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:label="yaki">


<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
Expand All @@ -35,6 +37,8 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />

<receiver android:name=".AlarmReceiver"/>
</application>
<!-- Provide required visibility configuration for API level 30 and above -->
<queries>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xpeho.yaki

import java.time.LocalDateTime

data class AlarmItem(
val time: LocalDateTime,
val message: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.xpeho.yaki

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import java.time.LocalDateTime

class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val message = intent?.getStringExtra("message") ?: return

if (context != null) {
// Show a push notification
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager

val channel =
NotificationChannel(
"Alarm",
"Alarm",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)

val notification =
NotificationCompat.Builder(context, "Alarm")
.setContentTitle("Reminder")
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.build()

notificationManager.notify(1, notification)

// Define the next alarm
if (intent.getBooleanExtra("repeat", false)) {
AlarmSchedulerImpl(context = context)
.schedule(
AlarmItem(
time =
LocalDateTime.now()
.plusDays(
1
),
message =
"It's time to do your daily declaration!"
)
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.xpeho.yaki

interface AlarmScheduler {
fun schedule(item: AlarmItem)
fun cancel(item: AlarmItem)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.xpeho.yaki

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import java.time.ZoneId

class AlarmSchedulerImpl(private val context: Context) : AlarmScheduler {

private val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

override fun schedule(item: AlarmItem) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {
val intent =
Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)
return
}
}

val intent =
Intent(context, AlarmReceiver::class.java).apply {
putExtra("message", item.message)
putExtra("repeat", true)
}
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
item.time.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
PendingIntent.getBroadcast(
context,
item.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}

override fun cancel(item: AlarmItem) {
alarmManager.cancel(
PendingIntent.getBroadcast(
context,
item.hashCode(),
Intent(context, AlarmReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
package com.xpeho.yaki

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.time.LocalDateTime

class MainActivity: FlutterActivity() {
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.xpeho.yaki/notification"

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call,
result ->
when (call.method) {
"scheduleNotifications" -> {
scheduleNotifications()
result.success(null)
}
"cancelNotifications" -> {
cancelNotifications()
result.success(null)
}
else -> result.notImplemented()
}
}
}

private fun scheduleNotifications() {
AlarmSchedulerImpl(context = this)
.schedule(
AlarmItem(
time = LocalDateTime.now().withHour(9).withMinute(0).withSecond(0),
message = "It's time to do your daily declaration!"
)
)
}

private fun cancelNotifications() {
AlarmSchedulerImpl(context = this)
.cancel(
AlarmItem(
time = LocalDateTime.now().withHour(9).withMinute(0).withSecond(0),
message = "It's time to do your daily declaration!"
)
)
}
}
4 changes: 3 additions & 1 deletion yaki_mobile/assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,7 @@
"zero": "Just now",
"one": "Since {} minute",
"other": "Since {} minutes ago"
}
},

"notificationStatus": "Daily notifications"
}
4 changes: 3 additions & 1 deletion yaki_mobile/assets/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,7 @@
"zero": "À l'instant",
"one": "Depuis {} minute",
"other": "Depuis {} minutes"
}
},

"notificationStatus": "Notifications journalières"
}
26 changes: 25 additions & 1 deletion yaki_mobile/lib/presentation/features/profile/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,31 @@ class Profile extends ConsumerWidget {
),
],
),
const SizedBox(height: 10),
const SizedBox(height: 20),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: Row(
children: [
const SizedBox(
width: 48,
child: Swap(
setActivated: true,
),
),
const SizedBox(width: 20),
Text(
tr('notificationStatus'),
style: const TextStyle(
color: kTextColor,
fontSize: 18,
fontWeight: FontWeight.w600,
fontFamily: 'SF Pro Rounded',
),
),
],
),
),
const SizedBox(height: 20),
InputText(
type: InputTextType.email,
label: tr('inputLabelFirstName'),
Expand Down
6 changes: 3 additions & 3 deletions yaki_mobile/pubspec.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yaki_mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ dependencies:
git:
url: https://github.com/XPEHO/yaki_ui.git
path: flutter
ref: 0.9.0
ref: 0.10.0
collection: ^1.17.2
image_picker: ^1.0.4
path_provider: ^2.1.1
Expand Down

0 comments on commit 5b19c42

Please sign in to comment.