【发布时间】:2018-02-05 23:01:02
【问题描述】:
我的应用向 firebase 注册推送通知,接收令牌,然后将该令牌作为 webview 加载的 url 的查询字符串参数提供。
虽然代码执行没有报错,但是在iOS 11中token始终为null。
以下代码适用于远程模拟器上的 iOS v9.x 或安装在设备上,但不适用于 iOS v11.x 时的任一环境。
有人经历过吗?有人有有用的建议吗?预先感谢您的帮助,我正在这里拔掉我剩下的头发。
我正在使用以下软件包/版本:
- Xamarin.Firebase.iOS.CloudMessaging v2.0.4.1
- Xamarin.Firebase.iOS.Core v4.0.13
- Xamarin.Firebase.iOS.InstanceId v2.0.8
来自 AppDelegate.cs:
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
// Register your app for remote notifications.
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// iOS 10 or later
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
System.Console.WriteLine(granted);
});
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
// For iOS 10 data message (sent via FCM)
Messaging.SharedInstance.Delegate = this;
}
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();
//configure firebase analytics.
App.Configure();
DoFCMConnect();
return true;
}
private void DoFCMConnect()
{
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
Console.WriteLine("Connected to FCM");
//removed when updated to newest firebase version
//Messaging.SharedInstance.Connect(error => {
// if (error != null)
// {
// System.Console.WriteLine(String.Format("Re-Connection to FCM Failed: {0}", error.ToString()));
// }
// else
// {
// System.Console.WriteLine("Re-Connected to FCM.");
// }
//});
}
来自 WebViewController.cs:
public override void ViewDidLoad()
{
string url = "https://www.mydomainname.com"; // NOTE: https secure request is required.
string token = InstanceId.SharedInstance.Token;
string fcm = Messaging.SharedInstance.FcmToken;
if (token == null && fcm != null)
{
token = fcm;
}
if (String.IsNullOrEmpty(token))
{
url += "/login";
}
else
{
url += "/app-in/{0}"; //page accepts token and associates it to the user after login.
url = String.Format(url, System.Web.HttpUtility.UrlEncode(token));
}
WebView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
}
字符串 token 和字符串 fcm 都包含 iOS 9 的值,但在 iOS 11 中两者都是 null。
【问题讨论】:
标签: ios firebase xamarin xamarin.ios