Flutter - 无法设置通知大图标

标签 flutter firebase-notifications flutter-notification

我正在使用:flutter_local_notifications:^5.0.0+3

我正在显示这样的通知,一切正常。自定义图标也可以使用。但是我正在尝试设置一个大图标,但它只是没有显示。这是没有任何大图标的通知屏幕截图:

更新 - 当我锁定手机时显示,并且通知显示在锁定屏幕上。但是,当它在屏幕上弹出时,它不显示。此外,当我向下滑动并查看列表中的所有通知时,它不会显示。仅在锁定屏幕上。为什么会这样?

No icon

这是我的代码:

class _MyAppState extends State<MyApp> {

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  FirebaseMessaging messaging = FirebaseMessaging.instance;

  @override
  void initState() {
    notificationPermission();
    initMessaging();

    subscribeToTopic('test');

    createChannel();


    super.initState();
  }

  // users unique token
  void getToken() async {
    print(await messaging.getToken());
  }


  @override
  Widget build(BuildContext context) {



    getToken();
    // showNotification();

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: Scaffold(

        appBar: AppBar(title: Text('AppBar Demo')),



      ),
    );
  }




  void notificationPermission() async {

    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    print('User granted permission: ${settings.authorizationStatus}');
  }

  void initMessaging(){

    // for notifications:
    var androidInit = AndroidInitializationSettings('my_icon');   // make sure this is in drawable folder in android

    var iosInit = IOSInitializationSettings();
    var initSetting = InitializationSettings(android: androidInit, iOS: iosInit);
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initSetting);

    // Set up listener

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Got a message whilst in the foreground!');
      print('Message data: ${message.data}');

      showNotification(message.data['title'], message.data['body']);

      if (message.notification != null) {
        print('Message also contained a notification title: ${message.notification.title}');
        print('Message also contained a notification body: ${message.notification.body}');
      }
    });

  }

  void showNotification(String title, String body) async {

    var androidDetails = AndroidNotificationDetails(
        '91512',
        'channelName',
        'channelDescription',
        importance: Importance.max,
        priority: Priority.high,
        visibility: NotificationVisibility.public,
        ticker: 'ticker',
        largeIcon: const DrawableResourceAndroidBitmap('my_icon'),

    );

    var iosDetails = IOSNotificationDetails();

    var generalNotificationDetails = NotificationDetails(android: androidDetails, iOS: iosDetails);

    await flutterLocalNotificationsPlugin.show(0, title, body, generalNotificationDetails, payload: 'Notification');


  }

  void subscribeToTopic(String topicName) async {
    await FirebaseMessaging.instance.subscribeToTopic(topicName).whenComplete(() =>
    {
      print('Sucessfully subscribed to topic: $topicName')
    });
  }

  void unsubscribeToTopic(String topicName) async {
    await FirebaseMessaging.instance.unsubscribeFromTopic(topicName);
  }

  Future<void> createChannel() async {

    // create channel:
    var androidNotificationChannel = AndroidNotificationChannel(
      '91512',  // channel ID
      'Your Channel name',    // channel name
      'Your Channel description', //channel description
      importance: Importance.high,

    );
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
        AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(androidNotificationChannel);

  }


}

最佳答案

我想你是在找AndroidNotificationDetails,给你举个例子:

AndroidNotificationDetails(
        channel.id,
        channel.name,
        channel.description,
        channelShowBadge: true,
        icon: '@mipmap/ic_icon',
        largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_largeIcon'),
),

正如您在通知解剖中看到的那样,不建议在 largeIcon 属性中设置应用程序图标:

Android Notification anatomy

使用 largeIcon 的正确方法是在发送通知时在通知负载中使用 image 属性:

const payload = {
        notification: {
          title: 'title',
          body: 'description',
          image: 'link-to-your-large-image',
          sound : "default",
          badge: "1"
        },
      };

Android Notifications

关于Flutter - 无法设置通知大图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67424371/

相关文章:

flutter - 如何将appState(提供者)传递到initState的屏幕中?

Android Firebase 推送通知在单击时重新启动应用程序

flutter : flutter_local_notifications onSelectNotification not working when app is terninated

flutter - 推送通知上的Flutter Show App图标徽章

flutter - 如何解决这个问题呢? ArgumentError(无效参数 : iOS settings must be set when targeting iOS platform. )

flutter - 如何显示 double 到 2 个小数点?

flutter - GradleException 显示红色表示是否有任何错误

flutter 陷入解决android的依赖关系

javascript - Firebase:用于推送通知的 Javascript SDK?

android - Firebase 发送推送通知两次