Skip to content

Commit

Permalink
check permission before post notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ichenhe committed Nov 11, 2023
1 parent 3856624 commit ab64181
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cc.chenhe.qqnotifyevo.core

import android.Manifest
import android.annotation.SuppressLint
import android.app.Notification
import android.content.Context
import android.service.notification.StatusBarNotification
Expand All @@ -9,6 +11,7 @@ import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import cc.chenhe.qqnotifyevo.utils.NotifyChannel
import cc.chenhe.qqnotifyevo.utils.Tag
import cc.chenhe.qqnotifyevo.utils.hasPermission
import kotlinx.coroutines.CoroutineScope
import timber.log.Timber
import java.util.*
Expand Down Expand Up @@ -60,8 +63,11 @@ class InnerNotificationProcessor(
}

private fun sendNotification(context: Context, tag: Tag, id: Int, notification: Notification) {
NotificationManagerCompat.from(context).notify(id, notification)
addNotifyId(tag, id)
@SuppressLint("MissingPermission")
if (context.hasPermission(Manifest.permission.POST_NOTIFICATIONS)) {
NotificationManagerCompat.from(context).notify(id, notification)
addNotifyId(tag, id)
}
}

override fun renewQzoneNotification(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cc.chenhe.qqnotifyevo.core

import android.Manifest
import android.annotation.SuppressLint
import android.app.Notification
import android.app.PendingIntent
import android.content.Context
Expand Down Expand Up @@ -51,7 +53,8 @@ class NevoNotificationProcessor(context: Context, scope: CoroutineScope) :
// 目前关联账号的消息都会合并
return
}
if (nevoMultiMsgTip(ctx)) {
@SuppressLint("MissingPermission")
if (nevoMultiMsgTip(ctx) && ctx.hasPermission(Manifest.permission.POST_NOTIFICATIONS)) {
val dontShow = PendingIntent.getBroadcast(
ctx, REQ_MULTI_MSG_DONT_SHOW,
Intent(ctx, StaticReceiver::class.java).also {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/cc/chenhe/qqnotifyevo/utils/ContextUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cc.chenhe.qqnotifyevo.utils
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat

/**
* @throws IllegalStateException can not find [Activity]
Expand All @@ -17,3 +19,6 @@ fun Context.getActivity(): Activity {
}
throw IllegalStateException("can not find Activity from current context")
}

fun Context.hasPermission(permission: String): Boolean =
ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED

0 comments on commit ab64181

Please sign in to comment.