Когда приложение регистрирует экстренное событие, даже если приложение находится в фоновом режиме и экран черный, я хочу, чтобы приложение выполнило одно из следующих действий:
- Предпочтительно: открыть основное действие приложения, где пользователь может выполнить действие.
- Альтернативный вариант: активировать экран и показать уведомление, чтобы пользователь знал о событии. выявлен
Мой первый подход таков:
Код: Выделить всё
private fun openApp() {
AppLogger.d(TAG, "Open App Intent")
val notificationManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val saveRequestOptionsBundle: Bundle? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val mode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
} else {
ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED
}
ActivityOptions.makeBasic().apply {
pendingIntentCreatorBackgroundActivityStartMode = mode
}.toBundle()
} else {
null
}
val openAppIntent = Intent(this, MainActivity::class.java)
val openAppPendingIntent = PendingIntent.getActivity(
this,
1,
openAppIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
saveRequestOptionsBundle
)
val notification = NotificationCompat.Builder(this, getString(R.string.emergency_notification_channel_id))
.setContentTitle(getString(R.string.emergency_notification_title))
.setFullScreenIntent(openAppPendingIntent, true)
.setContentText(getString(R.string.event))
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(openAppPendingIntent)
.setSmallIcon(R.drawable.ic_app_logo)
.setAutoCancel(false)
.build()
notificationManager.notify(11, notification)
}
Код: Выделить всё
private fun openApp1() {
Log.d(TAG, "Open App Intent")
val intent = Intent(this, MainActivity::class.java).apply {
addFlags(FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TOP)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) { // API 36+
val activityOptions = ActivityOptions.makeBasic().apply {
pendingIntentCreatorBackgroundActivityStartMode = ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
}
val launchActivityPendingIntent: PendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
activityOptions.toBundle()
)
try {
launchActivityPendingIntent.send()
} catch (e: PendingIntent.CanceledException) {
AppLogger.e(TAG, "PendingIntent was cancelled", e)
}
} else {
startActivity(intent)
}
}
Запуск фоновой активности заблокирован! [callingPackage: com.mypackage; вызовPackageTargetSdk: 36; Uid вызова: 10251; идентификатор вызова: -1; приложениеSwitchState: 1; вызовUidHasAnyVisibleWindow: ложь; вызовUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: ложь; ForceBalByPiSender: BackgroundStartPrivileges [allowsBackgroundActivityStars = true, разрешенийBackgroundForegroundServiceStars = true, originatingToken = android.os.Binder@8]; намерение: намерение { cmp=com.mypackage.MainActivity }; CallerApp: ноль; balAllowedByPiCreator: BSP.ALLOW_BAL; balAllowedByPiCreatorWithHardening: BSP.ALLOW_BAL; resultIfPiCreatorAllowsBal: BAL_BLOCK; имеетRealCaller: правда; isCallForResult: ложь; isPendingIntent: правда; autoOptInReason: ноль; realCallingPackage: com.samsung.android.wearable.sysui; RealCallingPackageTargetSdk: 33; RealCallingUid: 10; RealCallingPid: 1; RealCallingUidHasAnyVisibleWindow: ложь; RealCallingUidProcState: BOUND_FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: ложь; originatingPendingIntent: PendingIntentRecord{13ca919 com.mypackage startActivity (белый список: 87:+30s0ms/0/NOTIFICATION_SERVICE/NotificationManagerService)}; realCallerApp: ProcessRecord{55c 12:com.samsung.android.wearable.sysui/u0}; RealInVisibleTask: ложь; balAllowedByPiSender: BSP.ALLOW_BAL; resultIfPiSenderAllowsBal: BAL_BLOCK]
У меня есть все разрешения:
- USE_FULL_SCREEN_INTENT
- SYSTEM_ALERT_WINDOW
- POST_NOTIFICATIONS
- android:launchMode="singleTask"
- android:turnScreenOn="true"
- android:showWhenLocked="true"
Код: Выделить всё
// Emergency Channel
String emergency_channel_id = getString(R.string.emergency_notification_channel_id);
CharSequence emergency_name = getString(R.string.emergency_notification_channel_name);
String emergency_description = getString(R.string.emergency_notification_channel_description);
NotificationChannel emergency_channel = new NotificationChannel(
emergency_channel_id,
emergency_name,
NotificationManager.IMPORTANCE_HIGH
);
emergency_channel.setDescription(emergency_description);
emergency_channel.enableLights(true);
emergency_channel.setLightColor(Color.RED);
emergency_channel.enableVibration(true);
emergency_channel.setVibrationPattern(new long[]{0, 500, 200, 500, 200, 500});
emergency_channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
emergency_channel.setBypassDnd(true);
emergency_channel.setShowBadge(true);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
emergency_channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM), audioAttributes);
notificationManager.createNotificationChannel(emergency_channel);
Кто-нибудь понял это или знает, как лучше всего это сделать?>
Подробнее здесь: https://stackoverflow.com/questions/797 ... ion-from-m
Мобильная версия