iOS Development

swift – Push notification not engaged on iOS Flutter

Spread the love


Can not seem to figure-out show notifications on iOS, however works tremendous on Android.

  • Utilizing flutter_local_notification package deal
  • I’m utilizing a bodily system
  • Added the required code to appdelegate.swift

right here is my code for the cloud operate:

// Fetch the primary title of the consumer who created the appointment
const creatorSnapshot = await admin
  .firestore()
  .assortment("customers")
  .doc(appointment.createdBy)
  .get();
const creator = creatorSnapshot.knowledge();
const creatorName = creator ? creator.first_name : "Somebody";
const requestedUserId = appointment.createdBy;

// Get all customers to ship notification to, excluding the creator
const usersSnapshot = await admin.firestore().assortment("customers").get();
const tokens = [];
usersSnapshot.forEach((doc) => {
  if (doc.id !== appointment.createdBy) {
    const userToken = doc.knowledge().fcmToken;
    if (userToken) {
      tokens.push(userToken);
    }
  }
});

if (tokens.size === 0) {
  console.log("No legitimate tokens discovered. Notification not despatched.");
  return null;
}

// Setting up the FCM message
const message = {
  notification: {
    title: "New Session Out there",
    physique: `${creatorName} has created an appointment for ${appointment.exercises.be part of(
      ", "
    )} at ${appointment.date_time
      .toDate()
      .toLocaleString()}, are you accessible?`,
  },
  knowledge: {
    sort: "new_appointment",
    appointmentId: appointmentId,
    requestedUserId: requestedUserId,
  },
  tokens: tokens,
};

// Sending the FCM message
return admin
  .messaging()
  .sendMulticast(message)
  .then((response) => {
    console.log(response.successCount + " messages had been despatched efficiently");
  });

And right here is my code for dealing with the notifications

// iOS Initialization
var initializationSettingsIOS = DarwinInitializationSettings(
  requestAlertPermission: true,
  requestBadgePermission: true,
  requestSoundPermission: true,
  onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);

// request permissions
//for ios
if (Platform.isIOS) {
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          IOSFlutterLocalNotificationsPlugin>()
      ?.requestPermissions(
        alert: true,
        badge: true,
        sound: true,
      );
  //for android
} else if (Platform.isAndroid) {
  remaining AndroidFlutterLocalNotificationsPlugin? androidImplementation =
      flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>();

  // ignore: unused_local_variable
  remaining bool? grantedNotificationPermission =
      await androidImplementation?.requestNotificationsPermission();
}

// InitializationSettings for each platforms
var initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

saveUserTokenToFirestore();
print('step 1');

// initialize the plugins
await flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveNotificationResponse:
      (NotificationResponse notificationResponse) {
    if (notificationResponse.notificationResponseType ==
        NotificationResponseType.selectedNotification) {
      selectNotificationSubject.add(notificationResponse.payload);
    }
  },
);

onNotificationTap();

FirebaseMessaging.onMessage.hear((RemoteMessage message) {
  print('step 2');
  showNotification(message);
});

FirebaseMessaging.onMessageOpenedApp.hear((RemoteMessage message) {
  onNotificationTap();
});

// Android Notification Particulars
var androidDetails = const AndroidNotificationDetails(
  'new_appointment',
  'Appointment Notifications',
  channelDescription: 'Get notified about new appointments and updates',
  significance: Significance.max,
);

// iOS Notification Particulars
var iosDetails = const DarwinNotificationDetails(
    presentAlert: true, presentSound: true, subtitle: 'Notification');

var notificationDetails =
    NotificationDetails(android: androidDetails, iOS: iosDetails);

String payload = json.encode(message.knowledge);

// Show the notification utilizing the flutterLocalNotificationsPlugin
await flutterLocalNotificationsPlugin.present(
  message.hashCode,
  message.notification?.title ?? 'Default Title',
  message.notification?.physique ?? 'Default physique',
  notificationDetails,
  payload: payload,
);

Right here is my data.plist:

<key>FirebaseAppDelegateProxyEnabled</key> 
<false/>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>

Tried all of the information discovered on-line and nonetheless cant get notification to work on iOS

Leave a Reply

Your email address will not be published. Required fields are marked *