user1204563
user1204563

Reputation: 15

MissingPluginException(No implementation found for method createNewNotification on channel awesome_notifications)

I am trying to send a notification using Awesome notification plug in whenever a background task is successfully completed using Workmanager plug-in. Having the Workmanager work on iOS is a pain, but after days of de-bugging, it finally worked. When I do 'simulate background fetch' on xcode, I got notification that the 'perform fetch completed'. It worked as expected, every 15 minutes. Now, the problem is with the Awesome notification plug-in. I have followed the setup in here for iOS. This is my AppDelegate.swift

SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
              SwiftAwesomeNotificationsPlugin.register(
                with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
              SharedPreferencesPlugin.register( // replace here
                                with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
          }

    // Register background tasks only if not already registered
    if !AppDelegate.hasRegisteredBackgroundTask {
        WorkmanagerPlugin.registerBGProcessingTask(withIdentifier: "iOSBackgroundProcessing")

        // Register a periodic task in iOS 13+
        WorkmanagerPlugin.registerPeriodicTask(
            withIdentifier: "locationTracking",
            frequency: NSNumber(value: 15 * 60)
        )

        AppDelegate.hasRegisteredBackgroundTask = true
    }

And this is how my code is in main

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  AwesomeNotifications().initialize(
    // set the icon to null if you want to use the default app icon
      null,
      [
        NotificationChannel(
            channelGroupKey: 'basic_channel_group',
            channelKey: 'basic_channel',
            channelName: 'Basic notifications',
            channelDescription: 'Notification channel for basic tests',
            defaultColor: Color(0xFF9D50DD),
            ledColor: Colors.white)
      ],
      // Channel groups are only visual and are not required
      channelGroups: [
        NotificationChannelGroup(
            channelGroupKey: 'basic_channel_group',
            channelGroupName: 'Basic group')
      ],
      debug: true
  );


  // Only after at least the action method is set, the notification events are delivered
  AwesomeNotifications().setListeners(
      onActionReceivedMethod:         NotificationController.onActionReceivedMethod,
      onNotificationCreatedMethod:    NotificationController.onNotificationCreatedMethod,
      onNotificationDisplayedMethod:  NotificationController.onNotificationDisplayedMethod,
      onDismissActionReceivedMethod:  NotificationController.onDismissActionReceivedMethod
  );

  // Ensure the notification is created on the main thread
  SchedulerBinding.instance.addPostFrameCallback((_) {
    AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 10,
        channelKey: 'basic_channel',
        title: "Hello from WorkManager!",
        body: "This is a simple notification.",
      ),
    );
  });
  
  await Workmanager().cancelAll();
  await initializeWorkManager();
  Workmanager().registerPeriodicTask(
    "locationTracking", // Unique identifier for the task
    locationTracking, // Task name
    frequency: Duration(minutes: 15), 
  );
  Workmanager().printScheduledTasks();
  runApp(const MyApp());
}

Future<void> initializeWorkManager() async {
  await Workmanager().initialize(
    callbackDispatcher,
    isInDebugMode: true,
  );
  print("WorkManager initialization complete.");
}

@pragma('vm:entry-point')
void callbackDispatcher() {
  print("Workmanager task started.");
  Workmanager().executeTask((task, inputData) {
    switch(task){
      case locationTracking:
        print("Task excecuted: $task");
        AwesomeNotifications().createNotification(
          content: NotificationContent(
            id: 10,
            channelKey: 'basic_channel',
            title: "Hello from WorkManager!",
            body: "This is a simple notification triggered by the WorkManager task.",
          ),
        );
        print("Notification sent.");
    }
    return Future.value(true);
  });
}

Any suggestions or ideas how to fix it? I have been at this for too long now ...

Upvotes: 0

Views: 79

Answers (1)

Maciej Bastian
Maciej Bastian

Reputation: 311

MissingPluginException is a common exception when you add a package that heavily relies on native. Try:

  1. terminating your running app
  2. building it again

If it does not help, run flutter clean and flutter pub get and repeat the process. If error still occurs on iOS run following commands:

cd ios 
pod repo update
pod update --repo-update

and build the app again

Upvotes: 0

Related Questions