Приложение для Android работает отлично.
Проблема связана с собственным кодом приложения для iOS.
Пока приложение находится на переднем плане, оно будет получать неавтоматические уведомления от OneSignal и обрабатывает их в флаттер-коде. Это работает хорошо.
Я написал собственный код ios для получения тихих уведомлений (поскольку флаттер не получает их напрямую), и я передам уведомление в код флаттера.
Прямо сейчас я не получаю никаких уведомлений в собственном коде (когда приложение находится на переднем плане). Я настроил проект в соответствии с инструкциями здесь.
Я проверил настройку, чтобы включить фоновые уведомления.
Чтобы обойти OneSignal, я отправляю уведомления через консоль APN.
Это это мой родной код
AppDelegate.swift
Код: Выделить всё
import Flutter
import UIKit
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
print("**** didFinishLaunchingWithOptions ****");
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
print("**** didReceiveRemoteNotification ****");
DispatchQueue.main.async {
// Log or print messages here
print("Received silent notification: \(userInfo)")
}
// Process the silent notification
if let aps = userInfo["aps"] as? [String: Any],
aps["content-available"] as? Int == 1 {
// Call Flutter through a method channel
if let flutterViewController = window?.rootViewController as? FlutterViewController {
let channel = FlutterMethodChannel(name: "com.shachardev.vivencia/notifications", binaryMessenger: flutterViewController.binaryMessenger)
channel.invokeMethod("notificationReceived", arguments: userInfo)
}
completionHandler(.newData)
return
}
completionHandler(.noData)
}
}
Код: Выделить всё
import UserNotifications
import OneSignal
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var receivedRequest: UNNotificationRequest!
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
/* DEBUGGING: Uncomment the 2 lines below to check this extension is excuting
Note, this extension only runs when mutable-content is set
Setting an attachment or action buttons automatically adds this */
OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
print("Running NotificationServiceExtension")
//bestAttemptContent.body = "[Modified] " + bestAttemptContent.body
OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
OneSignal.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... foreground