观心静

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  420 随笔 :: 0 文章 :: 86 评论 :: 139万 阅读

查询权限

复制代码
    /**
     * 检查通知权限
     */
    private fun checkNotifyPermissionStatus() {
        context?.let { context ->
            val manager = NotificationManagerCompat.from(context)
            // areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。
            val isOpened = manager.areNotificationsEnabled()
            if (!isOpened) {
                mBinding.permissionTips.visibility = View.VISIBLE
                mBinding.goOpenPermission.visibility = View.VISIBLE
            } else {
                mBinding.permissionTips.visibility = View.GONE
                mBinding.goOpenPermission.visibility = View.GONE
            }
        }
    }
复制代码

引导用户授权

复制代码
    /**
     * 跳转到允许通知界面
     */
    private void jumpNoticeInterface() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getContext().getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getContext().getPackageName());
        }
        startActivity(localIntent);
    }
复制代码

创建通知

复制代码
            context?.let { context ->
                val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    val channel = NotificationChannel("nomal", "Nomal", NotificationManager.IMPORTANCE_DEFAULT)
                    manager.createNotificationChannel(channel)
                }
                val notification = NotificationCompat.Builder(context, "nomal")
                    .setContentTitle("this is title")
                    .setContentText("content")
                    .setSmallIcon(R.mipmap.sym_def_app_icon)
                    .build()
                manager.notify(1, notification)
            }
复制代码

携带PendingIntent的通知

复制代码
 val manager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
            val channel= NotificationChannel("nomal","Nomal", NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }
        sendAotice.setOnClickListener {
            val intent=Intent(this,NoticeActivity::class.java)
            val pi=PendingIntent.getActivity(this,0,intent,0)
            val notification=NotificationCompat.Builder(this,"nomal")
                .setContentTitle("this is title")
                .setContentText("this is content")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pi)
                .build()

            manager.notify(1,notification)
        }
复制代码

 

 

End

posted on   观心静  阅读(572)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
历史上的今天:
2019-01-21 Android 开发 Timer与TimerTask
2019-01-21 Android 开发 RecyclerView上拉加载更多功能实现
点击右上角即可分享
微信分享提示