ios - Xamarin.IOS 不接收 firebase 通知但注册 fcmToken
问题描述
您好,我需要一些建议,这可能是为什么我的通知被发送但我从未在手机上收到它们,而且它在物理设备上调试过 -> iPhone X (14.3)。
我已经按照这篇文章https://github.com/xamarin/GoogleApisForiOSComponents/blob/main/docs/Firebase/CloudMessaging/GettingStarted.md在 IOS 上实现 firebase 通知。
我将 APNs 身份验证密钥添加到我从开发人员 acc 下载的 firebase。
我在 info.plist 中添加了
- 属性 FirebaseMessagingAutoInitEnabled -> 否
- 选择启用后台模式和选择远程通知
在 GoogleService-Info.plist Build Action -> Bundle Resource
- 我仔细检查了我的 bundle_id,我的项目和 GoogleService-Info 中的相同。
在 Entitlements.plist 中选择远程通知
这是我的 AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
private App app;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Rg.Plugins.Popup.Popup.Init();
global::Xamarin.Forms.Forms.Init();
DependencyService.Register<ILocalStorageManager, LocalStorageManager>();
DependencyService.Register<ILauncherService, LauncherService>();
LoadApplication(new App());
Firebase.Core.App.Configure();
// Register your app for remote notifications.
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
Console.WriteLine(granted);
});
}
else
{
// iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
Messaging.SharedInstance.Delegate = this;
var token = Messaging.SharedInstance.FcmToken ?? "";
Console.WriteLine($"FCM token: {token}");
return base.FinishedLaunching(app, options);
}
[Export("messaging:didReceiveRegistrationToken:")]
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
Console.WriteLine($"Firebase registration token: {fcmToken}");
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);
// Print full message.
Console.WriteLine(userInfo);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);
// Print full message.
Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
// Receive displayed notifications for iOS 10 devices.
// Handle incoming notification messages while app is in the foreground.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
var userInfo = notification.Request.Content.UserInfo;
// With swizzling disabled you must let Messaging know about the message, for Analytics
//Messaging.SharedInstance.AppDidReceiveMessage (userInfo);
// Print full message.
Console.WriteLine(userInfo);
// Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptions.None);
}
// Handle notification messages after display notification is tapped by the user.
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
var userInfo = response.Notification.Request.Content.UserInfo;
// Print full message.
Console.WriteLine(userInfo);
completionHandler();
}
}}
在控制台中,它会生成我的 FCM 令牌,当我尝试从 Firebase 控制台发送通知时,控制台中没有打印任何内容,但它表示已完成。然后我尝试从邮递员发送它,我收到了下一条消息。
{
"multicast_id": 5300302940762876386,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1627386458132596%db948e52db948e52"
}
]}
我的 nugget 包 IOS.CloudMessaging、IOS.Core、iOS.Installations 和 iOS.IstanceID 是最新的。
解决方案
我发现了我的问题。
我覆盖了 FailedToRegisterForRemoteNotifications 并看到它抛出
“没有为应用程序找到有效的“aps-environment”权利字符串”
我检查了 info.plist 中的包标识符是否正常,但在我的 xCode 项目中是错误的,这解决了我的问题。
推荐阅读
- python - 一个 Wagtail 模型的多个 ModelAdmins
- r - 如何确定混合模型的第二个 beta 值,包括 H() 和 Geyer() 以使用 spatstat 进行模拟
- c# - 创建数据库克隆时出现异常
- python - 使用工厂模式时的条件参数
- python - Azure eventthub 到 NoSQL 数据库
- python - 多处理池挂起
- python - 将 metrics=["accuracy"] 添加到 keras 模型时出现 ValueError
- ios - AVAudioEngine 停止后连接 AVAudioUnitSampler 并再次启动崩溃
- flutter - Flutter:PageView保持停留在上一个屏幕
- docker - 在容器中执行一个脚本后如何退出容器