Iam sending notifications using Firebase Admin SDK for PHP and receiving in Flutter app using firebase and flutter_local_notification package.
My flutter app receiving notifications well in Android devices in foreground, background and terminated.
iOS devices receiving notifications only in foreground.
Notifications presents by flutter_local_notification package.
The problem that app not receiving notifications in ios device when the app in background and terminated.
Problem:
To get notifications in ios device when the app in background and terminated I need to set ‘alert’ => $data, then the app receiving notifications, BUT notifications sends directly to the device not to the app via flutter_local_notification.
I want to present notifications in all modes and devices via flutter_local_notification.
Is there any thing which disable notifications receiving in ios device when the app in background and terminate? How to solve it?
PHP
$data = [
'title' => $title,
'body' => $description,
'id' => $id,
];
$message = CloudMessage::withTarget('token', $user['firebase_token'])
// ->withNotification(Notification::create($title, $description))
->withData($data);
if(strtolower($user['platform']) == 'android'){
// Example from https://firebase.google.com/docs/cloud-messaging/admin/send-messages#android_specific_fields
$config = AndroidConfig::fromArray([
'ttl' => '3600s',
'priority' => 'normal',
'data' => [
'title' => $title,
'body' => $description,
'icon' => 'stock_ticker_update',
'color' => '#f45342',
'sound' => 'default',
],
]);
$message = $message->withAndroidConfig($config);
}
else if(strtolower($user['platform']) == 'ios'){
$config = ApnsConfig::fromArray([
'headers' => [
'apns-priority' => '5',
"apns-push-type" => "background",
"apns-topic" => "io.flutter.plugins.firebase.messaging", // bundle identifier
],
'payload' => [
'aps' => [
'contentAvailable'=> true,
// 'alert' => $data,
'badge' => 1,
'sound' => 'default',
"mutable-content" => 1,
],
'data' => $data,
],
]);
$message = $message->withApnsConfig($config);
}
try {
$messaging->send($message);
} catch (Exception $e) {
echo "Error sending message: " . $e->getMessage();
}
AppDelegate.swift
import UIKit
import Flutter
import flutter_local_notifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
GeneratedPluginRegistrant.register(with: registry)
}
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}