flutterfire
flutterfire copied to clipboard
[firebase_messaging]: Receiving notifications in iOS background/terminated not working
Is there an existing issue for this?
- [X] I have searched the existing issues.
Are you aware of the differences between iOS and Android background message handling?
- [X] I understand that iOS and Android background messages behave differently, and I've designed my application with that in mind.
Do you have an active Apple Developer account?
- [X] I have an active Apple Developer account.
Are you using a physical iOS device to test background messages?
- [X] I am using a physical iOS device to test background messages.
Have you enabled "Remote Notifications" & "Background Mode" (Checking options for "Background Processing" & "Remote Notifications") in your app's Xcode project?
yes
Have you created an APNs key in your Apple Developer account & uploaded this APNs key to your Firebase console?
yes
Have you disabled method swizzling for Firebase in your app?
Yes and tried with YES or No, it not works
Are you sending messages to your app from the Firebase Admin SDK?
Yes, lock at my comments
Have you requested permission from the user to receive notifications?
- [X] I have the relevant permission to receive notifications.
Have you used the 'Console' application on your macOS device to check if the iOS device's system is throttling your background messages?
No I didn't use it
Additional context and comments
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?
Flutter: 3.22.2
firebase_core: ^3.1.0 firebase_messaging: ^15.0.1 firebase_database: ^11.0.1 flutter_local_notifications: ^17.1.2
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'){
$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',
],
'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)
}
}