如何在Android设备上默认启用“显示弹出通知”?

问题描述 投票:0回答:1

我正在使用 Flutter 开发一款食品配送应用程序。我最近实现了 flutter_local_notifications 并且通知工作正常。但有一个问题是默认情况下通知不会显示为弹出窗口。默认情况下,通知设置中的“显示为弹出窗口”选项处于禁用状态。

有什么方法可以在安装应用程序时默认启用“显示为弹出窗口”选项。

这是我的通知配置代码:

void registerNotification() {
    // This function registers the user for recieving push notifications.
    // After registering the user, it creates a new field inside 'userForChat' Database
    // The field is called : 'pushToken' which is later used on to configure Firebase Automatic Cloud Messaging
    firebaseMessaging.requestNotificationPermissions();

    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {
        print('onMessage: $message');
        Platform.isAndroid
            ? showNotification(message['notification'])
            : showNotification(message['aps']['alert']);
        return;
      },
      onResume: (Map<String, dynamic> message) {
        print('onResume: $message');
        return;
      },
      onLaunch: (Map<String, dynamic> message) {
        print('onLaunch: $message');
        return;
      },
    );

    // Token for Firebase Messaging
    firebaseMessaging.getToken().then((token) {
      print('token: $token');

      Firestore.instance
          .collection('usersForChat')
          .document(currentUserId)
          .updateData(
              {'pushToken': token}); //Sets the firebase Token into the database
    }).catchError((onError) {
      setState(() {});
    });
  }

  void configLocalNotification() {

    var initializationSettingsAndroid = AndroidInitializationSettings(
        'mipmap/ic_launcher'); 
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  void showNotification(message) async {
    // This function takes the notfication message as input triggers the notification to show the message.
    // The input is in a json format so you have to decode the json with dart:convert.

    // IMPORTANT: Specify the Application package name according to the OS.
    //For Android, Use the android app package name from firebase
    //For iOS, Use the iOS app package name from firebase

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      // add your apps package name for each OS(Android:iOS)
      Platform.isAndroid
          ? 'com.jexmovers.app' //Update the package name to your app's package names
          : 'com.jexmovers.ios', //Update the package name to your app's package names
      'JexMovers Chat',
      'App that lets you contact with your food delivery person',
      playSound: true,
      enableVibration: true,
      importance: Importance.Max,
      priority: Priority.Max,
      visibility: NotificationVisibility.Public,
      enableLights: true,
    );

    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);

    print(message);

    print(message['body'].toString());
    print(json.encode(message));

    await flutterLocalNotificationsPlugin.show(
      0,
      message['title'].toString(),
      message['body'].toString(),
      platformChannelSpecifics,
      payload: json.encode(message),
    );
  }
android flutter push-notification firebase-cloud-messaging android-notifications
1个回答
0
投票

要默认为本地通知启用“显示为弹出窗口”选项,您可以包含 fullScreenIntent 参数

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
  Platform.isAndroid
      ? 'com.jexmovers.app'
      : 'com.jexmovers.ios',
  'JexMovers Chat',
  'App that lets you contact with your food delivery person',
  playSound: true,
  enableVibration: true,
  importance: Importance.Max,
  priority: Priority.Max,
  visibility: NotificationVisibility.Public,
  enableLights: true,
  fullScreenIntent: true, // Enable pop-up by default
);
© www.soinside.com 2019 - 2024. All rights reserved.