Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to push daily reminder notification everyday at 1… #3322

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change

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

2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
android:resource="@xml/wallet_balance_widget_info" />
</receiver>

<receiver android:name=".ReminderBroadcastReceiver" />

<service
android:name=".PaymentTileService"
android:exported="true"
Expand Down
78 changes: 78 additions & 0 deletions app/src/main/java/com/ivy/wallet/RootActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class RootActivity : AppCompatActivity(), RootScreen {
setupDatePicker()
setupTimePicker()

createNotificationChannel(this)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all users might want to have reminders, use IvyFeatures to make it a customizable setting that's enabled by default

scheduleDailyReminder(this)


AddTransactionWidget.updateBroadcast(this)
AddTransactionWidgetCompact.updateBroadcast(this)
WalletBalanceWidgetReceiver.updateBroadcast(this)
Expand Down Expand Up @@ -465,4 +469,78 @@ class RootActivity : AppCompatActivity(), RootScreen {
val addTransactionWidget = ComponentName(this, widget)
appWidgetManager.requestPinAppWidget(addTransactionWidget, null, null)
}

}
//For pushing Notification to the user as a reminder at every 12AM
// urging to fill in expenses, transactions and incomes
fun createNotificationChannel(context: Context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this logic in a separate class and module that lives outside :app. For example, :feature:reminders

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "DailyReminderChannel"
val descriptionText = "Channel for daily reminders"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel("DAILY_REMINDER_CHANNEL_ID", name, importance).apply {
description = descriptionText
}
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
fun sendNotification(context: Context) {
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

val builder = NotificationCompat.Builder(context, "DAILY_REMINDER_CHANNEL_ID")
.setSmallIcon(R.drawable.ic_launcher-playstore)
.setContentTitle("EveryDay Reminder")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract these a string so they can be localized

.setContentText("Fill in todays expences,transactions and incomes.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in expenses

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing @suyash01 🙌

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)

with(NotificationManagerCompat.from(context)) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code would simpler if:

  1. Extract the permission check as a hasNotificationsPermission function
  2. if (hasNotificationsPermisson()) notify

) {
return
}
notify(0, builder.build())
}
}
fun scheduleDailyReminder(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, ReminderBroadcastReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

val calendar: Calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, 12)
set(Calendar.MINUTE, 00)
hariom982 marked this conversation as resolved.
Show resolved Hide resolved
}

if (calendar.timeInMillis < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 1)
}

alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
}
class ReminderBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (context != null) {
sendNotification(context)
}
}

}

3 changes: 3 additions & 0 deletions build.gradle.kts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert these changes

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ plugins {
alias(libs.plugins.gradleWrapperUpgrade)

alias(libs.plugins.koverPlugin)


}


subprojects {
apply(plugin = "org.jetbrains.kotlinx.kover")
kover {
Expand Down
Loading