-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android_notif): Add daily notification in android platform and s…
…et the option in profile view (#1380)
- Loading branch information
Showing
12 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
yaki_mobile/android/app/src/main/kotlin/com/xpeho/yaki/AlarmItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
55 changes: 55 additions & 0 deletions
55
yaki_mobile/android/app/src/main/kotlin/com/xpeho/yaki/AlarmReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
yaki_mobile/android/app/src/main/kotlin/com/xpeho/yaki/AlarmScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
55 changes: 55 additions & 0 deletions
55
yaki_mobile/android/app/src/main/kotlin/com/xpeho/yaki/AlarmSchedulerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
} | ||
} |
46 changes: 45 additions & 1 deletion
46
yaki_mobile/android/app/src/main/kotlin/com/xpeho/yaki/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters