Xamarin.IOS 不接收 firebase 通知但注册 fcmToken
Posted
技术标签:
【中文标题】Xamarin.IOS 不接收 firebase 通知但注册 fcmToken【英文标题】:Xamarin.IOS does not receive firebase notifications but registers fcmToken 【发布时间】:2021-10-03 06:29:06 【问题描述】:您好,我需要一些建议,这可能是什么原因导致我的通知被发送但我从未在手机上收到它们,而且它在物理设备上调试过 -> iPhone X (14.3)。
我已关注这篇文章 https://github.com/xamarin/GoogleApisForiosComponents/blob/main/docs/Firebase/CloudMessaging/GettingStarted.md 在 IOS 上实现 firebase 通知。
我将 APNs 身份验证密钥添加到从我的开发者帐户下载的 firebase。
我在 info.plist 中添加了
属性 FirebaseMessagingAutoInitEnabled -> 否 选择启用后台模式和选择远程通知在 GoogleService-Info.plist 构建操作 -> 捆绑资源
我仔细检查了我的 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 是最新的。
【问题讨论】:
【参考方案1】:我发现了我的问题。
我重写了 FailedToRegisterForRemoteNotifications 并看到它抛出了
“没有为应用程序找到有效的“aps-environment”权利字符串”
我检查了 info.plist 中的包标识符是否正常,但在我的 xCode 项目中是错误的,这解决了我的问题。
【讨论】:
以上是关于Xamarin.IOS 不接收 firebase 通知但注册 fcmToken的主要内容,如果未能解决你的问题,请参考以下文章
在 Xamarin iOS 中使用 Firebase Crashlytics
将 Firebase Analytics 集成到带有钥匙串警告的 Xamarin iOS 垃圾邮件日志中
通知用户通过 UI xamarin iOS 在屏幕上接收来自服务器的更新时重新加载 uitableview 的最佳选择是啥?