![](/img/trans.png)
[英]How to show custom notification by default with collapsed layout when expand layout is enable in Android
[英]How to enable the Notification by default on android devices?
我最近實施了 flutter_local_notifications 並且通知工作正常。 但是有一個問題是默認情況下通知不會顯示為彈出窗口。 默認情況下,該選項在通知設置中處於禁用狀態。
我的代碼是這樣的:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
ServiceContainer.initialize(Injector());
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
await PreferenceHelper.create();
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
provisional: false,
);
await FirebaseMessaging.instance.requestPermission();
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(statusBarColor: Colors.transparent));
runApp(const App());
await [Permission.storage, Permission.notification].request();
}
您需要請求通知權限
請求 Android 13 或更高權限
從 Android 13(API 級別 33)開始,應用程序現在可以顯示提示,用戶可以在其中決定是否要授予應用程序顯示通知的權限。 有關此問題的進一步閱讀,請閱讀https://developer.android.com/guide/topics/ui/notifiers/notification-permission 。 要支持此應用程序,需要將其應用程序定位到 Android 13 或更高版本,並且編譯 SDK 版本需要至少為 33 (Android 13)。 例如,要以 Android 13 為目標,請將應用程序的 build.gradle 文件更新為 targetSdkVersion 為 33。然后,應用程序可以調用以下代碼來請求權限,其中 requestPermission 方法與 AndroidFlutterLocalNotificationsPlugin class(即插件的 8815603834098 實現)8 相關聯
這就是你如何做的。
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>().requestPermission();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.