Skip to content

Commit 1ecb0ca

Browse files
authored
Merge pull request #7977 from Stypox/error-notification-kitkat
Fix error notification on KitKat
2 parents 5459a55 + 2a9733f commit 1ecb0ca

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.error
22

33
import android.app.Activity
4-
import android.app.NotificationManager
54
import android.app.PendingIntent
65
import android.content.Context
76
import android.content.Intent
@@ -10,7 +9,7 @@ import android.os.Build
109
import android.view.View
1110
import android.widget.Toast
1211
import androidx.core.app.NotificationCompat
13-
import androidx.core.content.ContextCompat
12+
import androidx.core.app.NotificationManagerCompat
1413
import androidx.fragment.app.Fragment
1514
import com.google.android.material.snackbar.Snackbar
1615
import org.schabi.newpipe.R
@@ -105,13 +104,6 @@ class ErrorUtil {
105104
*/
106105
@JvmStatic
107106
fun createNotification(context: Context, errorInfo: ErrorInfo) {
108-
val notificationManager =
109-
ContextCompat.getSystemService(context, NotificationManager::class.java)
110-
if (notificationManager == null) {
111-
// this should never happen, but just in case open error activity
112-
openActivity(context, errorInfo)
113-
}
114-
115107
var pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT
116108
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
117109
pendingIntentFlags = pendingIntentFlags or PendingIntent.FLAG_IMMUTABLE
@@ -122,7 +114,13 @@ class ErrorUtil {
122114
context,
123115
context.getString(R.string.error_report_channel_id)
124116
)
125-
.setSmallIcon(R.drawable.ic_bug_report)
117+
.setSmallIcon(
118+
// the vector drawable icon causes crashes on KitKat devices
119+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
120+
R.drawable.ic_bug_report
121+
else
122+
android.R.drawable.stat_notify_error
123+
)
126124
.setContentTitle(context.getString(R.string.error_report_notification_title))
127125
.setContentText(context.getString(errorInfo.messageStringId))
128126
.setAutoCancel(true)
@@ -135,7 +133,8 @@ class ErrorUtil {
135133
)
136134
)
137135

138-
notificationManager!!.notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build())
136+
NotificationManagerCompat.from(context)
137+
.notify(ERROR_REPORT_NOTIFICATION_ID, notificationBuilder.build())
139138

140139
// since the notification is silent, also show a toast, otherwise the user is confused
141140
Toast.makeText(context, R.string.error_report_notification_toast, Toast.LENGTH_SHORT)

0 commit comments

Comments
 (0)