使用 FCM 的 Flutter 推送通知不适用于 iOS

问题描述 投票:0回答:2

我几周来一直在尝试让推送通知在 iOS 上正常工作,但没有成功。我已经仔细查看了文档来验证我的配置。不过,推送通知在 Android 上可以正常工作。

我还测试了直接从 firebase 消息控制台向 IOS 发送消息,但仍然不成功。我还尝试了之前的堆栈溢出帖子中的许多建议,但没有成功。

Flutter IOS FCM 推送通知未进入通知栏

Flutter 推送通知在 IOS 上不显示

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

iOS FirebaseCloudMessaging 通知在调试/测试飞行或发布中不起作用

我使用的是运行 iOS 14.6 的实体 iPhone 12。我使用的 Xcode 版本是 12.5。 Xcode配置如下。

签名和能力

签约

应用程序委托文件的代码

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>
ios flutter push-notification firebase-cloud-messaging
2个回答
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)
 }
}

在我的 Info.plist 中

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

还要确保在 Firebase 控制台中注册的应用程序与 Xcode 中使用的包标识符匹配。


0
投票

有多种情况会导致您没有收到提醒通知

最近对 SDK 进行更改后,您不必使用 fontawesomenotifications 等软件包手动显示推送通知,如果 SDK 将其识别为背景/通知,则 SDK 会自行处理后台和前台的平视通知类型,但是如果您使用的是早期的 SDK,则必须在前台消息侦听器中手动编写 fontawesomenotifications.show() 如果您在最新的 SDK 中编写此 fontawesomenotifications.show(),您可能会收到两次通知

我正在使用自定义 Django 后端来触发向我的 Flutter 应用程序发送推送通知,这是代码

请记住为 ios 添加 content_available=True 和 sound='default',以便 SDK 会将其视为通知类型并弹出带有声音的平视通知

这对我有用!!

© www.soinside.com 2019 - 2024. All rights reserved.