当前位置: 首页 > 知识库问答 >
问题:

如何在flutter中实现本地通知?

史鸿运
2023-03-14

我想用FCM和这个包实现本地通知,我是这样实现的:首先:我在我的项目上安装FCM和Flutter本地通知包,然后,我在Android系统中创建一个应用静态编程语言类-

申请代码:

    package com.example.onion
    
    import io.flutter.app.FlutterApplication
    import io.flutter.plugin.common.PluginRegistry
    import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
    import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
    import io.flutter.view.FlutterMain
    import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
    import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin
    
    class Application : FlutterApplication(), PluginRegistrantCallback {
    
        override fun onCreate() {
            super.onCreate()
            FlutterFirebaseMessagingService.setPluginRegistrant(this);
            FlutterMain.startInitialization(this)
        }
    
        override fun registerWith(registry: PluginRegistry?) {
            if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
                FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
            }
            if (!registry!!.hasPlugin("com.dexterous.flutterlocalnotifications")) {
                FlutterLocalNotificationsPlugin.registerWith(registry!!.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
            }
        }
    }

舱单代码:

<application
    android:name=".Application"
    android:icon="@mipmap/ic_launcher"
    android:label="onion">

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIza..." />


    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:launchMode="singleTop"
        android:showWhenLocked="true"
        android:theme="@style/LaunchTheme"
        android:turnScreenOn="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data
            android:name="io.flutter.embedding.android.NormalTheme"
            android:resource="@style/NormalTheme" />
        <meta-data
            android:name="io.flutter.embedding.android.SplashScreenDrawable"
            android:resource="@drawable/launch_background" />

        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
</application>

我也在稳定频道,但它不工作,我把频道改为beta,但仍然不工作

颤振医生:

    [√] Flutter (Channel beta, 1.24.0-10.2.pre, on Microsoft Windows [Version 10.0.14393], locale en-US)
        • Flutter version 1.24.0-10.2.pre at C:\src\flutter
        • Framework revision 022b333a08 (3 weeks ago), 2020-11-18 11:35:09 -0800
        • Engine revision 07c1eed46b
        • Dart version 2.12.0 (build 2.12.0-29.10.beta)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
        • Android SDK at C:\Users\Mahdi\AppData\Local\Android\Sdk
        • Platform android-30, build-tools 30.0.2
        • ANDROID_HOME = C:\Users\Mahdi\AppData\Local\Android\Sdk
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
        • All Android licenses accepted.
    
    [√] Android Studio (version 4.0)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin version 51.0.1
        • Dart plugin version 193.7547
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    
    [√] VS Code (version 1.51.1)
        • VS Code at C:\Users\Mahdi\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.17.0
    
    [√] Connected device (1 available)
        • Nexus 6 (mobile) • 127.0.0.1:62025 • android-x86 • Android 5.1.1 (API 22)
    
    • No issues found!

颤振pubspec。yaml文件:

      firebase_messaging: ^7.0.3
      flutter_local_notifications: ^3.0.2

main.dart代码:

    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          new FlutterLocalNotificationsPlugin();
      Future onSelectNotification(String payload) {
        print("Mahdi: onSelectNotification");
      }
    
      @override
      void initState() {
        super.initState();
        _fcm.getToken().then((value) => print("FCM TOKEN: $value"));
    
        var initializationSettingsAndroid =
            new AndroidInitializationSettings('@mipmap/ic_launcher');
        var initializationSettingsIOS = new IOSInitializationSettings();
        var initializationSettings = new InitializationSettings(
          android: initializationSettingsAndroid,
          iOS: initializationSettingsIOS,
        );
        flutterLocalNotificationsPlugin.initialize(
          initializationSettings,
          onSelectNotification: onSelectNotification,
        );
    
        _fcm.configure(
          onMessage: (Map<String, dynamic> message) async {
            showNotification(
              message['notification']['title'],
              message['notification']['body'],
            );
            print("onMessage: $message");
          },
          onLaunch: (Map<String, dynamic> message) async {
            print("onLaunch: $message");
            // TODO optional
            Navigator.pushNamed(
              context,
              message['data']['status'],
            );
          },
          onResume: (Map<String, dynamic> message) async {
            print("onResume: $message");
            // TODO optional
            Navigator.pushNamed(
              context,
              message['data']['status'],
            );
          },
        );
    
        if (Platform.isIOS) {
          dynamic iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {});
          _fcm.requestNotificationPermissions(IosNotificationSettings());
        }
      }
    
      void showNotification(String title, String body) async {
        await _demoNotification(title, body);
      }
    
      Future<void> _demoNotification(String title, String body) async {
        var androidPlatformChannelSpecifics = AndroidNotificationDetails(
            'channel_ID', 'channel name', 'channel description',
            importance: Importance.max,
            playSound: true,
            // sound: 'sound',
            showProgress: true,
            priority: Priority.high,
            ticker: 'test ticker');
    
        var iOSChannelSpecifics = IOSNotificationDetails();
        var platformChannelSpecifics = NotificationDetails(
          android: androidPlatformChannelSpecifics,
          iOS: iOSChannelSpecifics,
        );
        await flutterLocalNotificationsPlugin
            .show(0, title, body, platformChannelSpecifics, payload: 'test');
      }

但我有一个错误:

    no implementation found for method show on channel dexterous.com/flutter/local_notifications

我在谷歌上搜索了很多,发现了一些类似的问题,比如这个。但这对我不起作用。

共有1个答案

常自怡
2023-03-14

错误没有在通道dexterous.com.上找到方法显示的实现...发生在SDK的旧版本上,其中需要特定于Android的平台代码。如本指南所述,如果您使用最新的Flutter SDK版本和firebase_messaging包,则无需执行这些步骤。

 类似资料:
  • 点击输入框前: 点击输入框后:

  • 我是新来的。我想为我的flutter应用程序实现惰性加载功能,它使用sqflite数据库存储数据。在我的数据库中有1000+行。当应用程序打开时,我想加载10个项目。当我达到第10个数据时,然后加载下一个10个,以此类推。如何实现此功能??

  • 我已经用hibernate的@Audited注释审计了我的实体。它有一个简单的机制,默认为每个实体创建一个REVINFO表和一个_AUD,并在每个_AUD表中创建一个与REVINFO表相关的外键。但我想对本地进行修订。这意味着我要将修订版移动到each _AUD表并删除REVINFO表。 根据Hibernate envers页面 “本地修订”,即独立地为其创建修订。这不会启用关系的正确版本控制,但

  • 我正在尝试在我的Web应用程序(Flutter)中实现Stripe Payments,但我在https://pub.dev/中找不到任何软件包来满足我的愿望。对于我的iOS和Android应用程序,我https://pub.dev/packages/stripe_payment使用,但我在Flutter中看到了一些带有JS的Stripe Checkout教程。我的问题是,除了如何在Flutter

  • 我目前正在使用agora SDK进行flutter视频通话。在视频通话期间,我需要截图。只有在视频通话屏幕上,截图是黑色的,在其他屏幕上截图没有问题。通过使用RenderrePaintBarreden,我无法进行截图, 用于截图的代码: 问题截图: 我也尝试使用这个插件:屏幕截图:^0.1.1,但仍然出现问题