2
votes

I'm having problems integrating this plugin (4.0.4+300) into my existing flutter application. I always get the following error when initializing the plugin in this code:

TokenEventChannel.receiveBroadcastStream()
        .listen(_onTokenEvent, onError: _onTokenError);

The following MissingPluginException was thrown while activating platform stream on channel com.huawei.flutter.push/token: MissingPluginException(No implementation found for method listen on channel com.huawei.flutter.push/token)

I/flutter ( 7674): When the exception was thrown, this was the stack: I/flutter ( 7674): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7) I/flutter ( 7674): I/flutter ( 7674): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12) I/flutter ( 7674): #2 EventChannel.receiveBroadcastStream. (package:flutter/src/services/platform_channel.dart:519:29) I/flutter ( 7674): #3 EventChannel.receiveBroadcastStream.

I have followed this tutorial: https://medium.com/huawei-developers/sending-push-notifications-on-flutter-with-huawei-push-kit-plugin-534787862b4d

My application already includes the FCM push notification services, so I don't know if that could be a problem? I'd like to support both FCM and Push Kit on supported devices.

I'd appreciate any help resolving this issue.

Flutter doctor output is as follows: [✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.6 19G2021, locale en-ZA) • Flutter version 1.17.5 at /Users/user/Developer/flutter • Framework revision 8af6b2f038 (9 weeks ago), 2020-06-30 12:53:55 -0700 • Engine revision ee76268252 • Dart version 2.8.4

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3) • Android SDK at /Users/user/Library/Android/sdk • Platform android-30, build-tools 29.0.3 • ANDROID_HOME = /Users/samuel/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.6, Build version 11E708 • CocoaPods version 1.9.2

[✓] Android Studio (version 3.6) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 45.1.1 • Dart plugin version 192.8052 • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.48.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.13.2

[✓] Connected device (1 available) • Android SDK built for x86 64 • emulator-5554 • android-x64 • Android 9 (API 28) (emulator)

• No issues found! Process finished with exit code 0

My Gradle dependencies and plugins in app/build.gradle:

dependencies {
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
        implementation 'com.google.firebase:firebase-core:17.4.4'
        implementation 'com.google.firebase:firebase-messaging:20.2.3'
        implementation 'com.google.firebase:firebase-analytics:17.4.4'
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    
    }
    
    apply plugin: 'io.fabric'
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'kotlin-android'
    apply plugin: 'com.huawei.agconnect'

And in android/build.gradle:

dependencies {
    // Example existing classpath
    classpath 'com.android.tools.build:gradle:3.5.3'
    // Add the google services classpath
    classpath 'com.google.gms:google-services:4.3.2'
    classpath 'io.fabric.tools:gradle:1.26.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
1
This error is fixed on the 5.0.2+300 version of push kit pub.dev/packages/huawei_push.Ali Türkay Avci

1 Answers

1
votes

We have found out this error occurs on older Flutter projects that use the Android embedding v1. We have provided a fix already and it will be on the next release. If your project already supports v2 Embedding, Please inform us with a reply and we can investigate it further. Thank you for pointing out this issue.

To solve this error for now (before the next release), you must add Android Embedding v2 support to your project. I am leaving the steps for you to follow, these will hopefully solve your problem.

  1. Migrate your Flutter project to AndroidX if it hasn’t migrated already. You can follow the simple steps from The official AndroidX migration guide for Flutter.

  2. Add Flutter Embedding v2 meta data to your AndroidManifest.xml file Add the following flutterEmbedding meta-data under application tag of your AndroidManifest file as shown below

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.huawei.hms.flutter.push_example">
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:label="HMS Push Demo">
<activity
<!— 
Other configurations 
-->
</activity>
<!— Add the meta-data below. For supporting Android v2 Embedding
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
  1. Edit MainActivity.java Go to <your_flutter_project>/android/app/src/java/your_package_name/MainActivity.java and edit the MainActivity as shown below. Crucial part here is to import FlutterActivity Class from the io.flutter.embedding.android.FlutterActivity library.
package <your_package_name>;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {}

After these configurations you can run flutter clean and flutter run to make your project support the android v2 embedding. After running the commands, the new GeneratedPluginRegistrant.java file will be created by Flutter automatically.

  1. Check GeneratedPluginRegistrant.java file to make sure you use v2 embedding. Go to: <your_flutter_project>/android/app/src/java/io/flutter/plugins/GeneratedPluginRegistrant.java and check this file to make sure it is similar to the one shown below. If it is not similar, you can edit the file as below or delete and run flutter run command for Flutter to generate it again automatically. (Backup the file just in case if you are going to delete it).
package io.flutter.plugins;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;

import io.flutter.embedding.engine.FlutterEngine;

/**
* Generated file. Do not edit.
* This file is generated by the Flutter tool based on the
* plugins that support the Android platform.
*/
@Keep
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
flutterEngine.getPlugins().add(new com.huawei.hms.flutter.push.PushPlugin());
}
}

Now you have support for Android Embedding v2 and you should be able to obtain the token from the stream.