[英]Xamarin.iOS Firebase not receiving push notifications but receiving token
我在 Xamarin.Forms 項目中設置推送通知。 我已經為Xamarin做了所有工作這甚至不是我第一次這樣做,但我仍然無法弄清楚發生了什么。
我做了什么:
1. Included Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2 (which is not the latest, but the latest is not even building because of library errors)
2. 創建 Firebase 應用程序並按照通常的設置,上傳 my.p12 用於推送通知並添加我的團隊 ID。
3. 添加了我的 GoogleService-Info.plist 並將它的 BuildAction 設置為“BundleResource”
4.更新了我的Info.plist
5. 確保我在 Apple Developer Program 中的 App ID 包含推送通知和正確的證書
6.添加了與UserNotifications.IUNUserNotificationCenterDelegate、IMessagingDelegate接口相關的每一段代碼
7. 取回我的令牌
Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
var token = Messaging.SharedInstance.FcmToken;
if (!string.IsNullOrEmpty(token))
{
//AppData.Instance.TokenMobile = newToken;
}
});
我的 AppDelegate.cs
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, UserNotifications.IUNUserNotificationCenterDelegate, IMessagingDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
global::Xamarin.Forms.FormsMaterial.Init();
LoadApplication(new App());
Firebase.Core.App.Configure();
// For iOS 10 data message (sent via FCM)
Messaging.SharedInstance.Delegate = this;
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
// iOS 10 or later
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
if (granted)
{
InvokeOnMainThread(() =>
{
UIApplication.SharedApplication.RegisterForRemoteNotifications();
});
}
});
}
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();
// Handle token as you wish
Firebase.InstanceID.InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
var token = Messaging.SharedInstance.FcmToken;
if (!string.IsNullOrEmpty(token))
{
//AppData.Instance.TokenMobile = newToken;
}
});
UIApplication.SharedApplication.StatusBarHidden = true;
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
return base.FinishedLaunching(app, options);
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
}
[Export("messaging:didRefreshRegistrationToken:")]
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 FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
//Tested
//Messaging.SharedInstance.ApnsToken = deviceToken;
}
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);
}
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action boh)
{
}
[Export("messaging:didReceiveMessage:")]
public void DidReceiveMessage(Messaging messaging, RemoteMessage message)
{
}
}
我還將這個密鑰添加到 Entitlment.plist 文件中:
<dict>
<key>aps-environment</key>
<string>development</string>
</dict>
在這一點上,我期待在我的應用程序中收到一些東西,但我什么也收不到。
我在嘗試實現的每個回調中放置了一些斷點,但沒有一個被調用。
如果它有幫助,甚至“DidReceiveRegistrationToken”方法都不會被調用。
解決了這個問題。
我不知道為什么,但我在我的 GoogleService-Info.plist 中發現了一個與 Firebase 控制台中指定的密鑰不同的 API 密鑰。 我更新了密鑰,現在我正在接收通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.