【问题标题】:Flutter push notifications using FCM not working on iOS使用 FCM 的 Flutter 推送通知在 iOS 上不起作用
【发布时间】:2021-08-25 17:35:19
【问题描述】:

几周以来,我一直在尝试让推送通知在 iOS 上正常工作,但无济于事。我已经梳理了文档以验证我的配置。但是,推送通知在 Android 上可以正常工作。

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

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 not working in Debug / Test Flight nor Release

我在 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>

【问题讨论】:

  • 您是否尝试在通知 JSON 中添加 "content_available": true ?
  • @KathanPatel 是的,我做到了,但也没有用!
  • 您是否要向特定主题发送通知?如果是,那么您是否检查过您的 iOS 设备在订阅该主题时遇到任何错误。
  • 是的,我已经尝试订阅一个特定的主题,并成功推送到 Android 上。
  • @IsisCuriel 我也有同样的问题,你能解决吗?

标签: ios flutter firebase-cloud-messaging


【解决方案1】:

我终于想通了,但忘了发布答案! 在我的 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 中使用的包标识符匹配。

【讨论】:

  • 我也有这个 ios 推送通知的问题
  • 是的,我已成功发送到 IOS 14
  • 现在,如果我从 firebase 控制台发送正在通过但在前台本地推送通知不显示消息。
  • 我认为您必须配置一个设置才能在前台看到您的 Ios 通知。我相信您需要 alert :true 在以下配置中。
  • 感谢您,即使在我悬停鼠标时阅读有关属性的信息也能说明一切。谢谢
猜你喜欢
  • 2020-10-14
  • 2018-03-05
  • 2018-02-14
  • 2020-07-13
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多