gpt4 book ai didi

android - iOS 模拟器或物理设备未收到 FCM 数据消息

转载 作者:行者123 更新时间:2023-12-04 14:51:14 38 4
gpt4 key购买 nike

我正在从事的一个项目需要使用 FCM 推送通知和消息来控制 VoIP 等。

问题:

我的 iOS 物理设备接收推送通知,但仅当应用程序在后台或关闭时,但在前台时。此外,它根本不接收任何 fcm 消息(数据)通知,例如只有数据而没有通知标题/正文的消息,即 VoIP 状态消息。


我的设置步骤如下(可以找到我遵循的详细设置和一般流程here):

  1. 注册一个(Info.plist)(参见here)

  2. 下载并使用 Xcode 安装 GoogleService-Info.plist(参见 here)

  3. 创建 APNS key 并启用推送通知并将 .p8 文件上传到Firebase(参见 here)

  4. Swift代码修改:

AppDelegate.swift 代码:

import UIKit
import Flutter
import Firebase // this is added, and (see further down)

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure() //add this before the code below
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
  1. 添加功能到 iOS

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>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
...
<key>NSCameraUsageDescription</key>
<string>Allows taking a self-portrait profile image or sending a photo while chating</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Allows using your location to make determining country &amp; location picking easier (optional)</string>
<key>NSMicrophoneUsageDescription</key>
<string>Allows using micrphone to chat with someone when making a call</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Allows opening files from gallery</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>bluetooth-central</string>
<string>external-accessory</string>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
<string>voip</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<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>

注意:背景模式如图所示here无法启用,而是我使用一组复选框来启用它们以启用 Background Fetch 等。

  1. FCM - 收听和显示 以及使用某些通知(显示)服务(例如 awesome notifications)的通知/消息

实现遵循这些 guidelines :

前台消息:

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
...
});

背景信息:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();

print("Handling a background message: ${message.messageId}");
}

void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}

问题扩展:

调试时,如果 iOS 应用程序处于前台/后台并且测试通知或有效的 VoIP 调用(FCM 数据消息)被发送到设备,应用程序永远不会收到或(也不会命中所需的断点)。

什么会导致应用/iOS 收不到这些 FCM 消息?


从 Firebase 云功能服务发送的示例 VoIP 消息:

// build notification on liked status
const payload = {
token: fcmToken,
data: {
uid: context.auth.uid,
room_id: roomId,
...
},
android: {
priority: 'high',
ttl: 0,
notification: {
priority: 'max',
defaultSound: true,
defaultVibrateTimings: true,
sticky: true,
clickAction: "FLUTTER_NOTIFICATION_CLICK",
}
},
notification: {
title: `Incoming call from ${name}`,
body: `Accept/Reject call`,
// icon: 'your-icon-url', // Add profile image URL here if necessary
// clickAction: 'FLUTTER_NOTIFICATION_CLICK' // required only for onResume or onLaunch callbacks
},
apns: {
payload: {
aps: {
category: "NEW_MESSAGE_CATEGORY",
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
},
};

await admin.messaging().send(payload);

最佳答案

原始答案(更通用/对更多人有用):

FCM 不能在 iOS 模拟器上运行,所以问题已经解决了一半。对于物理设备,请查看一些 debugging steps我详细介绍了我工作的图书馆。

推送通知在 iOS 上出错的方式有多种。当您说:应用程序从不接收时,您的意思是您的 Flutter 应用程序没有收到消息吗?您的设备可能仍在接收它们。

  • 在您的 Mac 上打开 Console.app,然后开始记录您的物理设备。过滤dasd进程,你可以看到推送通知相关的日志,看看你的设备是否拒绝了推送通知,没有将它发送到你的应用程序。最有可能的是,您的信息结构存在问题。
  • 您还可以尝试在 Flutterfire firebase_messaging iOS 代码中调试 iOS 端的 didReceiveRemoteNotification 方法。 表示该消息已被该应用接收。

让我具体回答每个问题/疑虑 (3):

  1. 我的 iOS 物理设备收到推送通知,但仅当应用程序处于后台或关闭时但不是在前台

所以你想在前台显示通知?您需要实现 userNotificationCenter(_:willPresent:withCompletionHandler:) ,例如,在 Swift 中:

    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if #available(iOS 14.0, *) {
completionHandler(.banner)
} else {
completionHandler(.alert)
}
}

为了在这里为您提供更多帮助,您需要设置委托(delegate)。在上面实现的方法中放置一个断点,以查看它是否实际被调用。

  • 在 Swift 中:UNUserNotificationCenter.current().delegate = self;
  • 在 Objective-C 中:UNUserNotificationCenter.currentNotificationCenter.delegate = self;
  1. 此外,它根本不会收到任何 fcm 消息(数据)通知,例如只有数据而没有通知标题/正文的消息,即 VoIP 状态消息。

如果不设置notification字段,那么需要设置priority为5,push type为backgroundcontentAvailabletrue。不幸的是,您错误地使用了 firebase 中的 headers 字段。 apns 对象应如下所示,其中 header 位于 apns 内,因为这些是 apns 特定 header :

    apns: {
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
},
payload: {
aps: {
category: "NEW_MESSAGE_CATEGORY",
contentAvailable: true,
}
}
}

如果消息结构错误,您仍然可能在设备上收到错误消息,例如,告诉您优先级为 10(如果未设置,则默认为 10)。因为后台消息(无通知)不会按照文档传递,所以在 Console.app 中您应该看到:

CANCELED: com.apple.pushLaunch.com.example.my-example-app-bundle-id:3D8BB6 at priority 10 <private>!

Additionally, the notification’s POST request should contain the apns-push-type header field with a value of background, and the apns-priority field with a value of 5. - Pushing Background Updates to Your App

  1. 调试时,如果 iOS 应用程序处于前台/后台并且向设备发送了测试通知或有效的 VoIP 调用(FCM 数据消息),则应用程序永远不会收到或(也不会命中所需的断点)。什么会导致应用程序/iOS 收不到这些 FCM 消息?

这与#2 相同。

关于android - iOS 模拟器或物理设备未收到 FCM 数据消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69055482/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com