闪购增加推送通知
我的思路:
在闪购频道页添加推送按钮,默认为不订阅
点击订阅有两种情况:
1、用户没有打开权限,点击订阅弹出打开权限通知弹窗 若用户不打开权限 则状态不改变
2、用户已经打开了通知权限,点击订阅,状态改变并弹出toast
取消订阅:用户点击取消订阅,弹出选择窗口,keep remind则保持订阅状态,close remind则改变状态为未订阅
两个接口:
1、检查用户是否订阅:result==true 订阅 result==false 未订阅
2、保存用户的选择状态
代码实现:
保存订阅状态
当remindStatus==1时为订阅状态 remindStatus==0时为未订阅状态
当remindStatus==1并且接口返回true时订阅成功 否则订阅失败
isSubscribe = remindStatus == 1 && result as? Boolean ?: false (订阅失败)
用这个变量去判断订阅后的状态
if (remindStatus == 1) {
if (result == true) {
//订阅成功
ivStatus?.setImageDrawable(
ContextCompat.getDrawable(
this@FlashSaleActivity,
R.mipmap.ic_subscribed_flash_push
)
)
tvsubsrcption?.setText(R.string.subscribed)
Toast.makeText(
this@FlashSaleActivity,
R.string.Subscribed_a_flash_sales_reminder_will_be_sent,
Toast.LENGTH_LONG
).show()
} else {
if (result == true) {
//取消订阅成功
ivStatus?.setImageDrawable(
ContextCompat.getDrawable(
this@FlashSaleActivity,
R.mipmap.ic_add_flash_push
)
)
tvsubsrcption?.setText(R.string.Subscription)
}
}
点击订阅:
当isSubscribe==true时,判断权限,权限为开订阅成功,否则为默认状态
当isSubscribe==false时,取消订阅
if (!isSubscribe) {
if (!AppUtil.isNotificationEnable(this)) {
val dialog = NotificationPermissionDialog(this)
dialog.setListener {
isClickOpenNotification = true
}
dialog.setCancelable(false)
dialog.setCanceledOnTouchOutside(false)
dialog.show()
} else {
requestPust(1)
}
} else {
EzDialog(this).show {
title(R.string.Close_Reminder)
message(R.string.turning_off_flash_sales_reminder_may_miss_the_discount)
positiveButton(R.string.Keep_Reminder, click = {})
negativeButton(R.string.Close_Reminder, click = {
//开---关
requestPust(0)
ReportUtils.sendReportEvent(
ReportUtils.createRemindOffTOON("remind_me_switch_otf"),
"click"
)
})
cancelable(false)
}
}