Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: fcm ios push notifications doesn't work in release mode

I have bound my flutter-iOS app to firebase and i'm also using firebase-messaging and cloud functions for sending notifications via subscribing to topics, i'm using the APNs push notifications key of apple developer account. when i use the option runner>flutter run main.dart in release mode to build my app on my phone, fcm notifications doesn't work anymore, while it works in development mode, anyone can help me fix this?

this is my index.json code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
 
var newData;
 
exports.messageTrigger = functions.firestore.document('notifications/{notificationsId}').onCreate(async (snapshot, context) => {
newData = snapshot.data();
const payload = {
    notification: {
        title: newData.title,
        body: newData.body,
        sound: 'default'
    },
    
        data: {
      click_action: 'FLUTTER_NOTIFICATION_CLICK',
      message: newData.title,
    }

};

if (newData.language === 'english'){
    await admin.messaging().sendToTopic('english', payload);
}
else if (newData.language === 'arabic'){
    await admin.messaging().sendToTopic('arabic', payload);
}
else if (newData.language === 'kurdish'){
    await admin.messaging().sendToTopic('kurdish', payload);
}
});

hence package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
like image 687
azheen Avatar asked Sep 07 '20 18:09

azheen


People also ask

Can FCM send notification to iOS?

Once your client app is installed on a device, it can receive messages through the FCM APNs interface. You can immediately start sending notifications to user segments with the Notifications composer, or messages built on your application server.

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

Do push notifications work on iOS?

An iOS push notification is a message that pops up on an Apple device such as an iPhone. Before receiving push notifications from an app, iOS device users must explicitly give permission. Once a user opts-in, mobile app publishers can send push notifications to the users' mobile devices.

How to enable push notifications for iOS apps?

We now need to let iOS know about the fact that our apps wants to use push notifications. For that, select "Runner" in XCode and turn on "Push Notifications" and "Background Modes" in the "Signing & Capabilities" tab. Thereafter, under "Background Modes", check "Remote notifications" and "Background fetch".

How to send notifications to iOS device from Firebase?

I have the same issue. Looks like iOS release require additional notification params To check that notification works you could try to send message directly from the firebase console. Cloud Messaging -> Send your first message -> Enter notification title and text -> Send test message -> Enter your device token -> Test

How to send notifications to device using Xcode?

Cloud Messaging -> Send your first message -> Enter notification title and text -> Send test message -> Enter your device token -> Test To check release logs connect the device and open Xcode -> Window -> Devices and Simulators -> Open Console

How to send notifications to device using cloud messaging?

Cloud Messaging -> Send your first message -> Enter notification title and text -> Send test message -> Enter your device token -> Test To check release logs connect the device and open Xcode -> Window -> Devices and Simulators -> Open Console But I'm not sure which param helps: "apns-priority" or contentAvailable.


2 Answers

My project got the same issue. Combined two solutions I found, it finally works. (firebase_messaging 7.0.3)

As for debug mode, you don't need these.

Step 1: Edit AppDelegate.swift

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
    
    GeneratedPluginRegistrant.register(with: self)

    application.registerForRemoteNotifications()

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

Step 2: Edit ios/Runner/Info.plist. Add this:

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
like image 82
Derek Avatar answered Oct 24 '22 16:10

Derek


After update firebase_messaging to 7.0.0 I had the same problem. I added application.registerForRemoteNotifications() in my AppDelegate.swift and it worked!

like image 44
Maks K. Maks Avatar answered Oct 24 '22 16:10

Maks K. Maks