-
Notifications
You must be signed in to change notification settings - Fork 629
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,10 @@ class RootActivity : AppCompatActivity(), RootScreen { | |
setupDatePicker() | ||
setupTimePicker() | ||
|
||
createNotificationChannel(this) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all users might want to have reminders, use |
||
scheduleDailyReminder(this) | ||
|
||
|
||
AddTransactionWidget.updateBroadcast(this) | ||
AddTransactionWidgetCompact.updateBroadcast(this) | ||
WalletBalanceWidgetReceiver.updateBroadcast(this) | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract this logic in a separate class and module that lives outside |
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code would simpler if:
|
||
) { | ||
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) | ||
} | ||
} | ||
|
||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert these changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert this change