颤抖:fcm ios推送通知在发布模式下不起作用 [英] flutter: fcm ios push notifications doesn't work in release mode

查看:99
本文介绍了颤抖:fcm ios推送通知在发布模式下不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将flutter-iOS应用绑定到firebase,并且我还使用firebase消息传递和云功能通过订阅主题来发送通知,我使用了苹果开发者帐户的APNs推送通知键.当我在发布模式下使用选项运行器> flutter运行main.dart在手机上构建我的应用程序时,fcm通知将不再起作用,而在开发模式下则可以使用此功能吗?

这是我的index.json代码:

  const函数= 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有效负载= {通知: {标题:newData.title,正文:newData.body,声音:默认"},数据: {click_action:"FLUTTER_NOTIFICATION_CLICK",消息:newData.title,}};如果(newData.language ==='english'){等待admin.messaging().sendToTopic('english',payload);}否则,如果(newData.language ==='arabic'){等待admin.messaging().sendToTopic('arabic',有效负载);}否则,如果(newData.language ==='kurdish'){等待admin.messaging().sendToTopic('kurdish',有效负载);}}); 

因此package.json:

  {名称":功能",描述":"Firebase的云功能",脚本":{"lint":"eslint","serve":"firebase模拟器:start --only功能","shell":"firebase functions:shell","start":"npm run shell"," deploy":"firebase deploy --only functions",日志":"firebase功能:日志";},引擎":{节点":"10".},依赖项":{"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&";},私人":true} 

解决方案

我的项目遇到了同样的问题.结合我发现的两个解决方案,它终于成功了.(firebase_messaging 7.0.3)

对于调试模式,您不需要这些.

第1步:编辑AppDelegate.swift

 导入UIKit进口颤振@UIApplicationMain@objc类AppDelegate:FlutterAppDelegate {覆盖func应用程序(_应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:任何]?)->布尔{如果#available(iOS 10.0,*){UNUserNotificationCenter.current().delegate =自我为?UNUserNotificationCenterDelegate}GeneratedPluginRegistrant.register(with:self)application.registerForRemoteNotifications()返回super.application(应用程序,didFinishLaunchingWithOptions:launchOptions)}} 

第2步:编辑ios/Runner/Info.plist.添加此内容:

 < key> FirebaseAppDelegateProxyEnabled</key>< string> NO</string> 

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
}

解决方案

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>

这篇关于颤抖:fcm ios推送通知在发布模式下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆