【发布时间】:2017-05-05 08:27:02
【问题描述】:
由于某些原因,当应用在后台时,通过 Firebase 发送的通知不会进入托盘。这是初始化 Firebase 的代码(我们目前正在使用 VS 2017 中的 Xamarin 在 iOS 10 上进行测试)。在 AppDelegate.cs 中:
public void InitFirebase()
{
// Configure Firebase
App.Configure();
// 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) =>
{
Log.Info("BoaTan", "RequestAuthorization: {0}", granted);
});
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
// For iOS 10 data message (sent via FCM)
Firebase.CloudMessaging.Messaging.SharedInstance.RemoteMessageDelegate = this;
// Monitor token generation
InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
Log.Info("BoaTan", "New firebase token received {0}", PlatformEntrance.Token);
LoginViewModel viewModel = LoginView.Me.ViewModel as LoginViewModel;
viewModel.UpdateFirebaseToken(PlatformEntrance.Token);
});
Firebase.CloudMessaging.Messaging.SharedInstance.Connect(error =>
{
if (error != null)
{
Log.Error("BoaTan", error.DebugDescription);
}
else
{
Log.Info("BoaTan", "Connection to Firebase messaging succeeded");
}
});
// Monitor token generation
InstanceId.Notifications.ObserveTokenRefresh((sender, e) =>
{
SendTokenToServer();
});
}
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();
}
在 AppDelegate.cs 中我们也有以下代码来接收消息:
public override void WillEnterForeground(UIApplication application)
{
Firebase.CloudMessaging.Messaging.SharedInstance.Connect((NSError error) =>
{
Log.Info("BoaTan", "WillEnterForeground: Connected to Firebase messaging ({0})", error?.Description);
});
base.WillEnterForeground(application);
}
public override void DidEnterBackground(UIApplication application)
{
Firebase.CloudMessaging.Messaging.SharedInstance.Disconnect();
Log.Info("BoaTan", "DidEnterBackground: Disconnected from Firebase messaging");
base.DidEnterBackground(application);
}
// To receive notifications in foregroung on iOS 9 and below.
// To receive notifications in background in any iOS version
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
Log.Info("BoaTan", "DidReceiveRemoteNotification: Disconnected from Firebase messaging");
SendDataMessage(userInfo);
}
// To receive notifications in foreground on iOS 10 devices.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
Log.Info("BoaTan", "WillPresentNotification: Disconnected from Firebase messaging");
SendDataMessage(notification.Request.Content.UserInfo);
}
public void ApplicationReceivedRemoteMessage(RemoteMessage message)
{
SendDataMessage(message.AppData);
}
/// <summary>
/// Use MvvmCross messaging to send a message to subcribers.
/// </summary>
/// <param name="dictionary"></param>
private void SendDataMessage(NSDictionary dictionary)
{
LogReceivedInfo(dictionary);
NSObject data;
NSString key = new NSString("data");
if (dictionary.TryGetValue(key, out data))
{
Log.Info("BoaTan", "Data: {0}", data);
Settings.Notification = JsonConvert.DeserializeObject<LoginNotificationParameter>((NSString)data);
ServicesHelper.SendMessage(this);
}
}
private void LogReceivedInfo(NSDictionary keyvalues)
{
Log.Info("BoaTan", "-----------------------------------------------------------");
foreach (var keyval in keyvalues)
{
Log.Info("BoaTan", "Key: {0} Value: {1}", keyval.Key, keyval.Value);
}
Log.Info("BoaTan", "-----------------------------------------------------------");
}
}
当应用程序在前台时,消息完美到达。所有消息都会排队,直到应用再次进入前台。
这在 info.plist 中:
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
<string>fetch</string>
</array>
当我进入 Firebase 控制台并在那里以多种变体编写消息时,消息也不会到达托盘中,这使我得出以下结论:
- 应用缺少一些配置,告诉 iOS 我正在等待消息。
- Apple 开发者控制台中缺少 APN 配置。
- Firebase/iOS 配置/初始化中缺少某些内容。
排列是无穷无尽的。谁有答案?然后还有 iOS 9 的挑战。
【问题讨论】:
-
没人想吗?
标签: firebase apple-push-notifications ios10 firebase-cloud-messaging