由于某些原因,当应用在后台时,通过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 9的挑战。
答案 0 :(得分:0)
我没有在iOS上使用Firebase的经验,但在Android上我遇到了同样的问题。
Firebase有两种类型的消息:Notification message
和Data message
,请参阅About FCM Messages
在Android上,Notification message
仅在应用处于前台时可见。也许这也是你的问题