【发布时间】:2020-02-17 07:56:03
【问题描述】:
我正在 Xamarin.Forms 项目中设置推送通知。我已经为 Xamarin.Forms.Android 做了所有事情,它可以正常工作,但是我在 iOS 部分遇到了很多麻烦。这甚至不是我第一次这样做,但我仍然无法弄清楚发生了什么。
我做了什么:
1. 包含 Nuget Xamarin.Firebase.iOS.CloudMessaging v3.1.2(不是最新的,但由于库错误,甚至没有构建最新的)
2. 创建 Firebase 应用程序并按照常规设置,上传我的 .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;
}
});
- 回到 Firebase 控制台,创建了一个测试推送并尝试通过“Invia messagio di prova”(“发送消息”)发送它,这让我指定了一个令牌和通过发布。
我的 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”方法都不会被调用。
【问题讨论】:
-
确保您的项目中必须启用
Notification Capabilities。这里有一个类似的问题,也许可以帮助您stackoverflow.com/questions/54633523/…。
标签: c# ios firebase xamarin firebase-cloud-messaging