Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
启用和禁用android通知_Android_Notifications - Fatal编程技术网

启用和禁用android通知

启用和禁用android通知,android,notifications,Android,Notifications,我想创建一个具有切换(启用和禁用)按钮的通知应用程序。当我单击启用按钮时,通知将发出,而当我单击禁用按钮时,通知无法发送到应用程序或停止 如果开关处于打开状态,则调用.notify(id,builder.build()),否则请删除此行。MyFirebaseMessagingService类中的常见代码 val notificationBuilder=NotificationCompat.Builder(此) .setWhen(System.currentTimeMillis()) .setSh

我想创建一个具有切换(启用和禁用)按钮的通知应用程序。当我单击启用按钮时,通知将发出,而当我单击禁用按钮时,通知无法发送到应用程序或停止


如果开关处于打开状态,则调用.notify(id,builder.build()),否则请删除此行。

MyFirebaseMessagingService类中的常见代码

val notificationBuilder=NotificationCompat.Builder(此) .setWhen(System.currentTimeMillis()) .setShowWhen(真) .setSmallIcon(R.mipmap.ic_通知) .setContentIntent(挂起内容) .setContentTitle(标题) .setContentText(正文) .setLargeIcon(BitmapFactory.decodeResource(资源,R.mipmap.ic_启动器)) .setDefaults(Notification.DEFAULT\u声音或Notification.DEFAULT\u振动) .setColor(ContextCompat.getColor(applicationContext,R.color.colorPrimary)) .setAutoCancel(真) .setGroupSummary(真)

当开关打开时

notificationManager.notify(
        (System.currentTimeMillis() and 0xfffffff).toInt(),
        notificationBuilder.build()
    )

关闭开关时,请删除或在代码上方注释。

.notify(0,builder.build())
我认为使用
0
作为通知ID是不好的。实际上对此不确定。所以你有通知ID,通过它你可以取消通知。好吧,忘记了上面的代码。我想使用开关按钮启用和禁用通知。你能给出一些想法吗。正如我所说,使用你使用的通知ID来取消通知。
    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val NOTIFICATION_CHANNEL_ID = resources.getString(R.string.app_name)
        val importance = NotificationManager.IMPORTANCE_HIGH
        val notificationChannel =
            NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID, importance)
        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.BLUE
        notificationChannel.enableVibration(true)
        notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
        notificationManager.createNotificationChannel(notificationChannel)

    }
notificationManager.notify(
        (System.currentTimeMillis() and 0xfffffff).toInt(),
        notificationBuilder.build()
    )