3

I'm using windows laptop and android emulator and I also updated the build.gradle file. I'm getting an error at

var iOSPlatformChannelSpecifics = new IOSNotificationDetails();

in displayNotification method. Can anyone help me with this?

here is the code snippet code snippet

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';

class NotifyHelper {
   FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
     FlutterLocalNotificationsPlugin(); //

initializeNotification() async {
//tz.initializeTimeZones();
final DarwinInitializationSettings initializationSettingsDarwin =
    DarwinInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);

final AndroidInitializationSettings initializationSettingsAndroid =
    const AndroidInitializationSettings("appicon");

final InitializationSettings initializationSettings =
    InitializationSettings(
  iOS: initializationSettingsDarwin,
  android: initializationSettingsAndroid,
);
 await flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
}

displayNotification({required String title, required String body}) async {
print("doing test");
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    'your channel id', 'your channel name',
    importance: Importance.max, priority: Priority.high);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
// ignore: unnecessary_new
var platformChannelSpecifics = new NotificationDetails(
    android: androidPlatformChannelSpecifics,
    iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
  0,
  'You change your theme',
  'You changed your theme back !',
  platformChannelSpecifics,
  payload: 'It could be anything you pass',
);
}

void requestIOSPermissions() {
flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        IOSFlutterLocalNotificationsPlugin>()
    ?.requestPermissions(
      alert: true,
      badge: true,
      sound: true,
    );
}

  void onDidReceiveNotificationResponse(
    NotificationResponse notificationResponse) async {
    final String? payload = notificationResponse.payload;
   if (notificationResponse.payload != null) {
  debugPrint('notification payload: $payload');
} else {
  debugPrint("Notification Done");
}
Get.to(() => Container(
      color: Colors.white,
    ));
}

/*Future selectNotification(String? payload) async {
if (payload != null) {
  print('notification payload: $payload');
} else {
  print("Notification Done");
}
Get.to(() => Container(
      color: Colors.white,
    ));
}*/

 Future onDidReceiveLocalNotification(
  int id, String? title, String? body, String? payload) async {
// display a dialog with the notification details, tap ok to go to another page
/*showDialog(
  //context: context,
  builder: (BuildContext context) => CupertinoAlertDialog(
    title: Text(title),
    content: Text(body),
    actions: [
      CupertinoDialogAction(
        isDefaultAction: true,
        child: Text('Ok'),
        onPressed: () async {
          Navigator.of(context, rootNavigator: true).pop();
          await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => SecondScreen(payload),
            ),
          );
        },
      )
    ],
  ),
);*/
Get.dialog(const Text("Wellcome to flutter"));
}
}
Shreyas K S
  • 49
  • 1
  • 4

1 Answers1

8

Check the change log of the package. Version 10.0.0 introduced some breaking changes, and the guide you are following is probably earlier.

For example:

[iOS][macOS] Breaking changes iOS and macOS classes have been renamed and refactored as they are based on the same operating system and share the same notification APIs. Rather than having a prefix of either IOS or MacOS, these are now replaced by classes with a Darwin prefix. For example, IOSInitializationSettings can be replaced with DarwinInitializationSettings

So for your current issue the solution might be using DarwinNotificationDetails instead of IOSNotificationDetails. But there can be other issues as well.

Peter Koltai
  • 8,296
  • 2
  • 10
  • 20