max
max

Reputation: 1655

Flutter Firebase Messaging Not working on IOS when app running in background or closed

I have configured firebase Cloud Messaging with flutter Notification are working in foreground. but not working when running in background or app is killed.

Following steps are done.

Also tried to remove following line as suggested on Flutter Firebase Cloud Messaging - Notification when app in background but it is still not working.

if (@available(iOS 10.0, *)) { [UNUserNotificationCenter currentNotificationCenter].delegate = (id) self; }

Flutter Doctor

[✓] Flutter (Channel beta, v1.12.13+hotfix.6, on Mac OS X 10.14.5 18F132, locale en-IN)

[✗] Android toolchain - develop for Android devices

    ✗ Unable to locate Android SDK.

      Install Android Studio from: https://developer.android.com/studio/index.html

      On first launch it will assist you in installing the Android SDK components.

      (or visit https://flutter.dev/setup/#android-setup for detailed instructions).

      If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.

      You may also want to add it to your PATH environment variable.



[✓] Xcode - develop for iOS and macOS (Xcode 11.3)

[✓] Chrome - develop for the web

[!] Android Studio (not installed)

[✓] Connected device (3 available)

One thing I noticed that , when app is installed first time it is not asking me to check if the I allow the app to send push notification.

My code is having following lines on page after login page.

_firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });

Also cross checked that all following setting are ticked.

enter image description here

Upvotes: 44

Views: 62380

Answers (9)

max
max

Reputation: 1655

My Solution is as follows, and it works.

pubspec.yaml

firebase_messaging: ^6.0.9

Remove the following line from ios/runner/AppDelegate.m or ios/runner/AppDelegate.swift if used.

if (@available(iOS 10.0, *)) {
  [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
 }

In Dart code, use provisional false, Should get pop up to confirm on notification.

myFirebaseMessagingService.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: false));

In Xcode, Following Push notification along with background Fetch and remote notification.

enter image description here

Upvotes: 13

Nipun
Nipun

Reputation: 634

After spending couple of days, finally i found a solution. Please follow the below steps carefully

  1. Add this line to your info.plist FirebaseAppDelegateProxyEnabled : NO

  1. Add following code to the AppDelegate

2

 func application(application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
}
  1. In order to activate background handling in iOS, the following fields need to be included to the notification request:

"content_available": true

As a result, testing this functionality through the Firebase console is not possible. To achieve this, notifications must be sent using Postman.

  1. Add the following parameters to the headers of the request

Content-Type : application/json

Authorization: key = your_server_key

enter image description here

  1. Send notification though the Postman

request url :

https://fcm.googleapis.com/fcm/send

request body :

{
  "to" : "your_fcm_token",
  "notification" : {
 "body" : "sample body",
 "content_available" : true,
 "priority" : "high",
 "Title":"hello"
 }
}

This works for me !

Upvotes: 5

kashlo
kashlo

Reputation: 2618

In my case Team ID from apple developer account in Firebase Project settings for iOS app was incorrect

Upvotes: 0

Ziyad Sfaxi
Ziyad Sfaxi

Reputation: 360

Make sure that Push Notifications & Background Mods Capabilities were enabled in the All tab under Signing & Capabilities, not only in Profile like what I was doing.

enter image description here

https://github.com/FirebaseExtended/flutterfire/issues/1041#issuecomment-612660797

Upvotes: 8

JAHelia
JAHelia

Reputation: 7902

There is nothing to change or add in AppDelegate and Info.plist, just provide the correct notification payload and it will trigger:

sample valid payload

{
  "to" : "my_fcm_token",
  "notification" : {
 "body" : "sample body",
 "OrganizationId":"2",
 "content_available" : true,
 "mutable_content": true,
 "priority" : "high",
 "subtitle":"sample sub-title",
 "Title":"hello"
 },
  "data" : {
    "msgBody" : "Body of your Notification",
    "msgTitle": "Title of your Notification",
    "msgId": "000001",
    "data" : "This is sample payloadData"
  }
}

Upvotes: 3

thanhbinh84
thanhbinh84

Reputation: 18434

I have similar issue, I need to revoke the old key, create a new authentication key and upload again to make it works:

https://firebase.google.com/docs/cloud-messaging/ios/certs#create_the_authentication_key https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_authentication_key

Upvotes: 2

protalor
protalor

Reputation: 11

In my case, I solved it as below.

Complete the tutorial guide and try the following:

  • Edit : iOS/Flutter/AppFrameworkInfo.plist

    • <key>CFBundleIdentifier</key> <string>{bundleIdentifier}.flutter</string>
    • {bundleIdentifier} is: Enter the Bundle name set at https://developer.apple.com.
  • Example

    • Developer.apple: com.example.app
    • xcode BundleIdentifier : com.example.app
    • iOS/Flutter/AppFrameworkInfo.plist : com.example.app.flutter
  • Test environment :

    • Firebase_core: ^0.4.5
    • Firebase_messaging: ^6.0.16
  • (Additional) When sending from your server or page

    • Please set the "notification" item.

Upvotes: 0

Feisal  Aswad
Feisal Aswad

Reputation: 455

I managed to get my app working and listening to onBackgroundMessage on Android (I just removed the notification object from my payload, leaving only the data object).

    final data = {
  "registration_ids": userToken,
  "click_action": "FLUTTER_NOTIFICATION_CLICK",
  "priority": "high",
  "collapse_key": "type_a",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "other_data": "any message",
    "title": 'NewTextTitle',
    "body": 'NewTextBody',
    "priority": "high",
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
  },
};

Upvotes: 0

dcg
dcg

Reputation: 791

After a long time searching for a fix, this solved the problem for me:

  1. In your info.plist:

     FirebaseAppDelegateProxyEnabled: false
    
  2. Add this to your AppDelegate:

    override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
       Messaging.messaging().apnsToken = deviceToken
       super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
     }
    

Upvotes: 22

Related Questions