我已将flutter-iOS应用绑定到firebase,并且我还使用firebase消息传递和云功能通过订阅主题来发送通知,我正在使用Apple开发人员帐户的APNs推送通知键。当我使用选项Runner> flutter在发布模式下运行main.dart在手机上构建我的应用程序时,fcm通知将不再起作用,而在开发模式下则可以用任何人帮助我解决此问题吗?
这是我的index.json代码:
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);
}
});
因此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
}
答案 0 :(得分:0)
我有同样的问题。似乎iOS版本需要其他通知参数
要检查通知是否有效,您可以尝试直接从Firebase控制台发送消息。
云消息传递->发送您的第一条消息->输入通知标题和文本->发送测试消息->输入设备令牌->测试
要获取设备令牌,可以使用print(await FirebaseMessaging().getToken());
要检查发布日志,请连接设备并打开Xcode->窗口->设备和模拟器->打开控制台
如果可行,您可以尝试添加以下参数:
const payload = {
notification: {
title: newData.title,
body: newData.body,
sound: 'default'
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
message: newData.title,
},
apns: {
headers: { "apns-priority": "5" },
payload: {
aps: {
contentAvailable: true,
category: "NEW_MESSAGE_CATEGORY"
}
}
},
};
但我不确定"apns-priority"
或contentAvailable
是哪个参数有用。
答案 1 :(得分:0)
在将firebase_messaging更新为7.0.0之后,我遇到了同样的问题。我在AppDelegate.swift中添加了application.registerForRemoteNotifications()并成功了!
答案 2 :(得分:0)
我的项目遇到了同样的问题。结合我发现的两个解决方案,它终于可以工作了。 (firebase_messaging 7.0.3)
对于调试模式,您不需要这些。
第1步:编辑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)
}
}
第2步:编辑ios / Runner / Info.plist。添加此内容:
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>