Flutter使用FCM推送通知在iOS上无法工作。

3
我已经尝试了数周来使iOS上的推送通知正常工作,但一直没有成功。我已经仔细查阅了文档以验证我的配置。然而,安卓上的推送通知正常工作。
我还尝试了直接从Firebase消息控制台向iOS发送消息,但仍然没有成功。我也尝试了许多之前在Stack Overflow上的建议,但都没有成功。 Flutter IOS FCM push notification not coming into notification bar Flutter Push notification not displaying on IOS

https://github.com/FirebaseExtended/flutterfire/issues/1677

iOS FirebaseCloudMessaging Notifications 在调试/测试模式和发布模式下无法工作

我正在使用运行在 iOS 14.6 上的 iPhone 12。 我所使用的 Xcode 版本是 12.5。 Xcode 的配置如下。

签名和功能 Signing and Capabilities

签名 Signing

AppDelegate 文件的代码

import UIKit
import Flutter
import Firebase
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
  override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   Messaging.messaging().apnsToken = deviceToken
   super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
 }
}

请求推送通知的代码

Future<void> notficationsPermission () async {
  FirebaseMessaging messaging = FirebaseMessaging.instance;

NotificationSettings settings = await messaging.requestPermission(
  alert: true,
  announcement: true,
  badge: true,
  carPlay: false,
  criticalAlert: true,
  provisional: false,
  sound: true,
);


print('User granted permission: ${settings.authorizationStatus}');


String uid = Pref.getString(Keys.USER_ID);
var databaseReference = FirebaseDatabase.instance.reference();
if(settings.authorizationStatus == AuthorizationStatus.authorized){
  notficationStatus = true; 
 await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
   
  alert: true, // Required to display a heads up notification
  badge: true,
  sound: true,
);
}

else{
  notficationStatus = false;  
}
}
}

如何配置通知的片段
return admin.messaging().sendToTopic(
                            topicName, {
                              android: {
                                priority: "high",
                              },
                              // Add APNS (Apple) config
                              apns: {
                                payload: {
                                  aps: {
                                    contentAvailable: true,
                                  },
                                },
                                headers: {
                                  "apns-push-type": "background",
                                  "apns-priority": "5", // Must be `5` when `contentAvailable` is set to true.
                                  "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
                                },
                              },
                              notification: {
                                title: snapshot2.val().group_name +
                                  ": new chat message",
                                body: name +":"+snapshot.val().message,
                                clickAction: "FLUTTER_NOTIFICATION_CLICK",
                              },
                            });

我在我的Info.plist文件中也有以下内容。
<key>FirebaseAppDelegateProxyEnabled</key>
    <string>0</string>

你尝试在通知JSON中添加 "content_available": true 了吗? - Kathan Patel
@KathanPatel 是的,我试过了,也不起作用! - Isis Curiel
你是否正在尝试向特定主题发送通知?如果是的话,您是否检查过您的iOS设备在订阅该主题时是否遇到任何错误。 - Kathan Patel
是的,我已经尝试订阅特定主题,并且在Android上成功推送。 - Isis Curiel
@IsisCuriel 我也遇到了同样的问题,请问你解决了吗? - DNS
@DNS 我已经做了!我马上会发布答案! - Isis Curiel
1个回答

3

我终于弄清楚了,但是忘记发布答案!在我的index.js文件中。

exports.chatNotfi = functions.database.ref("messages/{gId}/{chat}")
.onCreate((snapshot, context)=>{
  const groupId = context.params.gId;
  console.log("Group id:" + groupId);
  const topicName = groupId + "chat";
  console.log("topic name"+topicName);
  const userId = snapshot.val().userId;
  return admin.database().ref("groups/"+groupId+ "/").once("value").
      then((snapshot2)=>{
       
                    return admin.messaging().sendToTopic(
                        topicName, {
                          notification: {
                            title:
                              ": New chat message",
                            body: name +":"+snapshot.val().message,
                            clickAction: "FLUTTER_NOTIFICATION_CLICK",
                          },
                        });
});

在我的AppDelegate.swift文件中

import UIKit
import Flutter
import Firebase
import FirebaseMessaging

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: 
 [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: 
  launchOptions)
  }
  override func application(_ application: UIApplication, 
  didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   Messaging.messaging().apnsToken = deviceToken
   super.application(application, 
   didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
 }
}

In my Info.plist

    <key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

同时确保在firebase控制台注册的应用程序与Xcode使用的包标识符匹配。


我也遇到了iOS推送通知的问题。 - Gbenga B Ayannuga
是的,我成功地发送到IOS 14。 - Isis Curiel
现在,如果我从Firebase控制台发送消息,它可以正常传递,但是在前台时本地推送通知却没有显示消息。 - Gbenga B Ayannuga
我认为你需要配置一些设置才能在前台看到你的iOS通知。我相信你需要在以下配置中添加alert:true。 - Isis Curiel
请等待FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, // 必须显示悬浮通知 badge: true, sound: true, ); - Isis Curiel
1
谢谢您阅读这篇关于属性的文章,当我将鼠标悬停在上面时,它就能告诉我一切。谢谢。 - Gbenga B Ayannuga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接