我正在为Android和iOS开发一款Flutter应用程序。它具有通知,因此我确实实现了firebase_messaging API。我正在通过设备ID向某些设备发送通知,也向topics
发送通知。
我正在测试topics
通知发送,在Android中可以100%正常运行。我按照上面提供的链接中的指南进行操作,还实现了iOS设置。但是在iOS中,发送通知时,只有在前台时,应用程序才能接收该通知。这意味着只能通过onMessage
。如果该应用程序处于后台或关闭状态,则看不到任何通知(我正在控制台中进行打印)。但是当我重新打开该应用程序时,通知会被打印。
下面是我的通知注册代码
FirebaseMessagesImpl msg = FirebaseMessagesImpl();
msg.requestPermissions();
//Get configured with firebase messaging to recieve messages
msg.getMessage().then((_) {
msg.register().then((String token) {
print(token);
//Register to the `topic` so we get messages sent to the topic
msg.topicRegister();
.........
}
FirebaseMessagesImpl
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
class FirebaseMessagesImpl {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
String serverToken =
"AAAAAAA:XXXXXXXXX-YYYYYYYY-ZZZZ-BBBBBBBB";
Future<String> register() async {
String token = "";
_firebaseMessaging.getToken().then((t) {
token = t;
print("ha: " + t);
});
return token;
}
Future<void> getMessage() async {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
//setState(() => _message = message["notification"]["title"]);
}, onResume: (Map<String, dynamic> message) async {
print('on resume $message');
// setState(() => _message = message["notification"]["title"]);
}, onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
//setState(() => _message = message["notification"]["title"]);
});
}
void topicRegister() {
// _firebaseMessaging.subscribeToTopic("mobile_admin");
_firebaseMessaging.subscribeToTopic("puppies");
}
void requestPermissions() async
{
await _firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound:true, badge:true, alert:true, provisional:false)
);
}
}
以下是我的 Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>MY APP NAME</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
下面是 AppDelegate.swift
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}
正如其他人在其他答案中所建议的那样,我从AppDelegate.swift代码中删除了以下部分并进行了尝试,但仍然存在相同的问题。
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
我也已按照指南将苹果密钥文件上传到Firebase项目cloud messaging
部分。
下面是我的JSon代码发送通知
{
"notification": {
"body": "body",
"title": "title"
},
"priority": "high",
"data": {
"body": "body",
"title": "title",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
"image": "https://ibin.co/2t1lLdpfS06F.png"
},
"to": "/topics/puppies"
}
我的登录功能如下
我是iOS的新手,无法知道发生了什么。我想就像在Android中一样,当应用程序处于后台或关闭状态时,通知会自动显示在通知栏中。
更新
根据@nandish的建议,我如下更改了AppDelegate.swift
文件
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
UNUserNotificationCenter.current().delegate = self
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
// MARK: - UNUserNotificationCenterDelegate Method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
}
然后我遇到以下错误
UNUserNotificationCenter' is only available in iOS 10.0 or newer
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
更新
在前台,发出通知时,我从应用程序收到以下响应。那也只能在iPhone Simulator中使用,而不能在真实设备中使用。
{from: /topics/puppies, status: done, id: 1, notification: {body: body, title: title, e: 1, tag: topic_key_9204266xxxxxxx535}, title: title, image: https://ibin.co/2t1lLdpfS06F.png, collapse_key: com.aaa.xxx, body: body, click_action: FLUTTER_NOTIFICATION_CLICK}
答案 0 :(得分:1)
首先,如果您使用Firebase Cloud Messaging进行通知,则Android将在应用打开或关闭时收到通知。
但在iOS方面,iOS正在使用APN's (Apple Push Notification)
。
*-您应该在真实设备上尝试。在iOS模拟器上,获取通知并不稳定。在*
在Firebase控制台上,转到Settings > Cloud Messaging > iOS Application Configuration
,然后从Apple Developer面板添加APN标识密钥。您可以在this document上看到。
此外,您还应该在通知有效载荷上添加"content_available": true
。
这里有一个例子:
{
"to": "/topics/topic_name",
"content_available": true,
"notification": {
"title": "TITLE TEXT",
"body": "BODY TEXT",
"content_available": true
},
"data": {
"body": "BODY TEXT",
"title": "TITLE TEXT",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}
答案 1 :(得分:0)
对于后台通知,您可以尝试在目标项目的“签名与功能”下单击“ +功能” ,以添加“背景模式” 功能。 “ 标签,并按照下面的Apple文档中的建议启用远程通知。
还应在AppDelegate.swift中添加以下内容:
class AppDelegate: FlutterAppDelegate , UNUserNotificationCenterDelegate {
override func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
....
UNUserNotificationCenter.current().delegate = self
....
}
....
// MARK: - UNUserNotificationCenterDelegate Method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
}
答案 2 :(得分:0)
我在firebase_messaging 6.0.16中发现一个问题时遇到了一个非常相似的问题,现在它已更新为7.0.0
我调整了info.plist并进行了更改
<key>FirebaseAppDelegateProxyEnabled</key><false/>
to <key>FirebaseAppDelegateProxyEnabled</key>
<string>false</string>
由于我没有升级到较新的通知,因此我的通知在firebase_6.0.9中正常工作。
我的appdelegate是:
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
//FirebaseApp.configure()
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
它与您的有所不同。
答案 3 :(得分:0)
您是否尝试过投射传递到onMessage回调的消息数据,
data = Map<String, dynamic>.from(message['data']);