flutter 【厉害了】(9522):无效通知(没有有效的小图标)

ghg1uchk  于 2023-10-22  发布在  Flutter
关注(0)|答案(3)|浏览(166)

我试图在我的flutter项目中添加通知,当我运行该项目时,显示此错误:
E/Android:Awesome无效通知(没有有效的小图标):通知(pri=2 contentView=com.example.test/0x1090085 vibrate=null sound=content://settings/system/notification_sound tick defaults=0x0 flags=0x11 color=0xff000000维斯=PRIVATE)(NotificationThread:58)
mainscreen.dart

import 'package:flutter/material.dart';
import 'package:test/page/notification_service.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key:key);

  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  @override
  void initState(){
    // TODO: implement initState
    super.initState();
    NotificationService().requestPermission();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GestureDetector(
        
        onTap: (){
          NotificationService().showNotification(
          1,
          'main_channel',
          'Test title',
          'Test body',
          );
        },
        child: Container(
          height: 30,
          width: 70,
          color: Colors.blue,
          child: Text(
            "SHOW NOTIFICATION"
          ),
        ),
      ) ,
        GestureDetector(
              onTap: () {
                NotificationService().showScheduledNotification(
                  1,
                  'main_channel',
                  'Test title',
                  'Test body',
                  5,
                );
              },
                 child: Container(
                height: 30,
                width: 70,
                color: Colors.red,
                child: Text(
                    "SHOW NOTIFICATION2"
                ),
              ),
        ),
        ],
      ),
      ),
    );
  }
}

notification_service.dart

import 'package:awesome_notifications/awesome_notifications.dart';

class NotificationService{
  static final NotificationService _notificationService = NotificationService._internal();
  
  factory NotificationService(){
    return _notificationService;
  }
  NotificationService._internal();

Future<void> initAwesomeNotification()async{
  AwesomeNotifications().initialize(
    'resource://drawable/ic_applogo',
    [
      NotificationChannel(
        channelKey: 'main_channel', 
        channelName: 'main_channel', 
        channelDescription: 'main_channel notifications',
        enableLights: true,
        importance: NotificationImportance.Max,
        )
    ],
  );
}
Future<void> requestPermission() async{
  AwesomeNotifications().isNotificationAllowed().then((allowed){
    if(!allowed){
      AwesomeNotifications().requestPermissionToSendNotifications();

    }
  });

}
Future<void> showNotification(int id,String channelKey,String title,String body) async{
  AwesomeNotifications().createNotification(
    content: NotificationContent(
      id: id,
      channelKey: channelKey,
      title: title,
      body: body,
    ),
  );
}

 Future<void> showScheduledNotification(int id, String channelKey, String title, String body, int interval) async {
    String localTZ = await AwesomeNotifications().getLocalTimeZoneIdentifier();

    AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: id,
        channelKey: channelKey,
        title: title,
        body: body,
      ),
      schedule: NotificationInterval(
        interval: interval,
        timeZone: localTZ,
        repeats: false,
      )
    );
  }
}
klh5stk1

klh5stk11#

这个问题可能是由logo引起的:
试着替换这个:'resource://drawable/ic_applogo',与以下null,

6qftjkof

6qftjkof2#

所以你可以插入另一个图标,至少在Android上我是这样做的:我删除了所有的“ic_launcher.png”,因为我没有使用它作为我的应用程序的图标的默认值,正是它没有让资源://工作。之后,我重新安装了应用程序,并为我想要使用的另一个图标设置了目录,例如:resource://drawable/icon_app,那么它就工作了,我没有在lib/assets文件夹中测试它的图标,但它可能也工作了。

zmeyuzjn

zmeyuzjn3#

我修复了这个问题,用一个带有透明背景的图标替换它,并用flutter_launcher_icons将它导入到项目中

相关问题