我已经将我的应用程序Xamarin.iOS与Firebase Cloud Messaging集成在一起。当应用程序处于前台或我在后台应用程序时,推送通知正常工作,然后返回应用程序,但当应用程序处于后台或被杀死时不起作用。
这是我的代码
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
Firebase.Core.App.Configure();
UNUserNotificationCenter.Current.Delegate = this;
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// For iOS 10 display notification (sent via APNS)
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
Console.WriteLine(granted);
});
}
else
{
// iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
Messaging.SharedInstance.Delegate = this;
UIApplication.SharedApplication.RegisterForRemoteNotifications();
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
var token = Messaging.SharedInstance.FcmToken;
Rg.Plugins.Popup.Popup.Init();
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
当应用程序处于前台时运行良好的方法
[Export("messaging:didReceiveRegistrationToken:")]
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
// Monitor token generation: To be notified whenever the token is updated.
LogInformation(nameof(DidReceiveRegistrationToken), $"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.
}
[Export("messaging:didReceiveMessage:")]
public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
// Handle Data messages for iOS 10 and above.
// HandleMessage(remoteMessage.AppData);
var notification = (NSDictionary)remoteMessage.AppData.ValueForKey(new NSString("notification"));
var title = notification.ValueForKey(new NSString("title"));
var text = notification.ValueForKey(new NSString("body"));
remotenotification = true;
ScheduleNotification(title.ToString(), text.ToString());
}
//This code is for showing notification
void ScheduleNotification(string title, string body)
{
// Create content
var content = new UNMutableNotificationContent();
content.Title = title;
//content.Subtitle = "Subtitle";
content.Body = body;
content.Badge = 1;
content.CategoryIdentifier = "notification_fv";
content.Sound = UNNotificationSound.Default;
// Fire trigger in one seconds
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
var requestID = "customNotification";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
// This is the line that does the trick
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
if (err != null)
{
// Report error
System.Console.WriteLine("Error: {0}", err);
}
else
{
// Report Success
System.Console.WriteLine("Notification Scheduled: {0}", request);
}
});
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
if (!remotenotification)
return;
SystemSound.Vibrate.PlayAlertSound();
SystemSound.Vibrate.PlaySystemSound();
completionHandler(UNNotificationPresentationOptions.Alert);
remotenotification = false;
}
我从FCM作曲家那里收到的有效载荷
{{
"collapse_key" = "com.app.myApp";
from = 933033592921;
notification = {
body = for;
e = 1;
tag = "campaign_collapse_key_6180700435185093924";
title = Testing;
};
}
谁能告诉我我在这里错过了什么?
已解决的问题: 当我实现此方法时发现问题,它给我抛出了一个错误找不到应用程序的有效"APS 环境"权利字符串
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
var alert = new UIAlertView("Computer says no", "Notification registration failed! Try again!", null, "OK", null);
alert.Show();
}
然后我解决了这个问题,因为我没有在我的 iOS 捆绑签名选项 ->自定义权利中输入 Entitlement.plist。添加此文件后,现在在所有条件(前台,后台以及应用程序已被破坏(下都会接收通知(带有通知有效负载(,这可能也会帮助某人:)。
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
//Background
completionHandler(UIBackgroundFetchResult.NewData);
}
它在后台获取通知对我有用。