将通知设置为在某个用户事件时触发
Posted
技术标签:
【中文标题】将通知设置为在某个用户事件时触发【英文标题】:setting the Notification to trigger at a certain user event 【发布时间】:2021-10-13 11:11:45 【问题描述】:所以我正在创建这个轮播应用程序并且我正在处理通知。我能够创建通知并在片段开始时弹出通知,但我想做的是让通知每 20 次左右滑动弹出一次。
这是通知的代码
片段
class MainFragment: Fragment()
// private lateinit var manager: RecyclerView.LayoutManager
private lateinit var viewPager: ViewPager2
private var counter: Int = 0
private var pageChangeCallback: ViewPager2.OnPageChangeCallback? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
val binding: MainFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.main_fragment,
container, false)
viewPager = binding.imageSlider
val data: MutableList<CarouselItem> = ArrayList()
data.add(CarouselItem(1, bcw_65, "First"))
data.add(CarouselItem(2, bcw_66, "Second"))
data.add(CarouselItem(3, bcw_67, "Third"))
data.add(CarouselItem(4, bcw_68, "Fourth"))
data.add(CarouselItem(5, bcw_69, "Fifth"))
data.add(CarouselItem(6, bcw_70, "Sixth"))
data.add(CarouselItem(2, bcw_71, "Seventh"))
viewPager.adapter = CarouselItemAdapter(data, viewPager)
viewPager.clipToPadding = false
viewPager.clipChildren = false
viewPager.offscreenPageLimit = 3
viewPager.getChildAt(0).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
val compositePageTransformer = CompositePageTransformer()
compositePageTransformer.addTransformer(MarginPageTransformer(30))
compositePageTransformer.addTransformer page, position ->
val r = 1 - abs(position)
page.scaleY = 0.85f + r * 0.25f
viewPager.setPageTransformer(compositePageTransformer)
//Used to count the number of swipes bing called
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback()
override fun onPageSelected(position: Int)
counter++
binding.counterView.text = counter.toString()
.also pageChangeCallback = it )
createChannel(
getString(R.string.carousel_notification_id),
getString(R.string.app_name)
)
val notificationManager = ContextCompat.getSystemService(
requireContext(),
NotificationManager::class.java,
) as NotificationManager
notificationManager.sendNotification(
requireContext().getText(R.string.swipe_description).toString(), requireContext()
)
return binding.root
//Needed to destroy the pageChangeCallback
override fun onDestroyView()
super.onDestroyView()
viewPager.unregisterOnPageChangeCallback(pageChangeCallback!!)
pageChangeCallback = null
private fun createChannel (channelID: String, channelName: String)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
val notificationChannel = NotificationChannel(
channelID,
channelName,
NotificationManager.IMPORTANCE_HIGH
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.WHITE
notificationChannel.enableVibration(true)
notificationChannel.description = getString(R.string.swipe_description)
val notificationManager = requireActivity().getSystemService(
NotificationManager::class.java
)
notificationManager.createNotificationChannel(notificationChannel)
通知实用程序
private val NOTIFICATION_ID = 0
fun NotificationManager.sendNotification(messageBody: String, applicationContext: Context)
val builder = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.carousel_notification_id)
)
.setSmallIcon(R.drawable.with_outline_6)
.setContentTitle(applicationContext.getString(R.string.app_name))
.setContentText(messageBody)
notify(NOTIFICATION_ID, builder.build())
我知道可能有一个我现在看不到的简单解决方案。任何帮助表示赞赏
【问题讨论】:
***.com/questions/35500049/… 【参考方案1】:我知道怎么做。在 Viewpager.registerOnPageChangeCallback 调用之前,我必须首先向上移动频道通知以及通知管理器。这样我就可以使用通知实例将通知设置为在特定时间出现。这绝对是一个 if 语句,但是将前两个部分向上移动是需要发生的事情。然后使用模块操作让通知显示每 20 次滑动。
【讨论】:
以上是关于将通知设置为在某个用户事件时触发的主要内容,如果未能解决你的问题,请参考以下文章
当 S3 和 Lambda 位于不同区域时,如何使用 S3 事件通知触发跨区域 Lambda 函数