|
| 1 | +package com.sameerasw.airsync.crash |
| 2 | + |
| 3 | +import android.app.NotificationChannel |
| 4 | +import android.app.NotificationManager |
| 5 | +import android.app.PendingIntent |
| 6 | +import android.content.Context |
| 7 | +import android.content.Intent |
| 8 | +import android.os.Build |
| 9 | +import androidx.core.app.NotificationCompat |
| 10 | +import androidx.core.app.NotificationManagerCompat |
| 11 | +import com.sameerasw.airsync.R |
| 12 | + |
| 13 | +object CrashNotificationHelper { |
| 14 | + private const val CHANNEL_ID = "crash_report" |
| 15 | + const val NOTIFICATION_ID = 9999 |
| 16 | + |
| 17 | + fun createChannel(context: Context) { |
| 18 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 19 | + val name = context.getString(R.string.crash_notification_title) |
| 20 | + val descriptionText = context.getString(R.string.crash_notification_body) |
| 21 | + val importance = NotificationManager.IMPORTANCE_LOW |
| 22 | + val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { |
| 23 | + description = descriptionText |
| 24 | + setSound(null, null) |
| 25 | + enableLights(false) |
| 26 | + enableVibration(false) |
| 27 | + } |
| 28 | + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager |
| 29 | + notificationManager.createNotificationChannel(channel) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + fun postNotification(context: Context) { |
| 34 | + // Broadcast pending intent for Dismiss |
| 35 | + val dismissIntent = Intent(context, CrashNotificationReceiver::class.java).apply { |
| 36 | + action = "com.sameerasw.airsync.action.DISMISS_CRASH_NOTIFICATION" |
| 37 | + } |
| 38 | + val dismissPendingIntent = PendingIntent.getBroadcast( |
| 39 | + context, |
| 40 | + 1, |
| 41 | + dismissIntent, |
| 42 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
| 43 | + ) |
| 44 | + |
| 45 | + // Activity pending intent for Report |
| 46 | + val reportIntent = Intent(context, CrashReportActivity::class.java).apply { |
| 47 | + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK |
| 48 | + putExtra("show_report", true) |
| 49 | + } |
| 50 | + val reportPendingIntent = PendingIntent.getActivity( |
| 51 | + context, |
| 52 | + 2, |
| 53 | + reportIntent, |
| 54 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
| 55 | + ) |
| 56 | + |
| 57 | + val builder = NotificationCompat.Builder(context, CHANNEL_ID) |
| 58 | + .setSmallIcon(R.drawable.rounded_bug_report_24) |
| 59 | + .setContentTitle(context.getString(R.string.crash_notification_title)) |
| 60 | + .setContentText(context.getString(R.string.crash_notification_body)) |
| 61 | + .setPriority(NotificationCompat.PRIORITY_LOW) |
| 62 | + .setSilent(true) |
| 63 | + .setAutoCancel(true) |
| 64 | + .setContentIntent(reportPendingIntent) |
| 65 | + .addAction( |
| 66 | + 0, |
| 67 | + context.getString(R.string.crash_action_dismiss), |
| 68 | + dismissPendingIntent |
| 69 | + ) |
| 70 | + .addAction( |
| 71 | + 0, |
| 72 | + context.getString(R.string.crash_action_report), |
| 73 | + reportPendingIntent |
| 74 | + ) |
| 75 | + |
| 76 | + try { |
| 77 | + val notificationManager = NotificationManagerCompat.from(context) |
| 78 | + notificationManager.notify(NOTIFICATION_ID, builder.build()) |
| 79 | + } catch (e: SecurityException) { |
| 80 | + e.printStackTrace() |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments