繁体   English   中英

应用程序在后台时推送通知不起作用

[英]Push notifications not working while app is in background

在我的 Xamarin.iOS 应用程序中,我有推送通知工作,所以我收到一个通知/横幅,我可以点击它,它会打开一个 UIViewController。 在我的 AppDelegate 的FinishedLaunching方法中:

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
    // For iOS 10 display notification

    var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
    UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
    {
        Console.WriteLine(granted);
    });
    UNUserNotificationCenter.Current.Delegate = this;
}
else
{
    // iOS 9 or before
    var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    var FCMsettings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings(FCMsettings);
}

UIApplication.SharedApplication.RegisterForRemoteNotifications();

Messaging.SharedInstance.Delegate = this;

然后我实现WillPresentNotificationDidReceiveMessage但它们只有在我的应用程序处于前台时才会被调用。 如果它在后台,我只会在再次打开应用程序时看到横幅。 我想在应用程序处于后台时看到横幅,然后当我点击它时,它会打开我的应用程序。 我怎样才能做到这一点?

这是我创建通知/横幅的代码:

UNMutableNotificationContent notification = new UNMutableNotificationContent();
notification.Title = title;
notification.Body = msg;
notification.CategoryIdentifier = channel;
var requestID = channel;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false);
var request = UNNotificationRequest.FromIdentifier(requestID, notification, trigger);
              UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
              {
                if (err != null)
                {
                    // TODO
                }
              });

它从我的 AppDelegate 中的DidReceiveMessage调用,并在屏幕顶部显示几秒钟,但如果我的应用程序在后台,则在我的应用程序再次进入前台之前不会调用DidReceiveMessage 只有这样我才能看到它。

我的 AppDelegate 实现UserNotifications.IUNUserNotificationCenterDelegateFirebase.CloudMessaging.IMessagingDelegate 以下是它实现的一些功能:

[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public async void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
}

[Export("messaging:didReceiveMessage:")]
public async void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
    // Look at remoteMessage.AppData key/value pairs and present notification if needed
}

[Export("messaging:didReceiveRemoteNotification:fetchCompletionHandler:")]
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    // check userInfo key/value pair and display notification if needed
}

您可以将应用程序配置为在后台更新。 然后将后台更新推送到您的应用程序。 更多详细信息,您可以参考以下文档:

在后台更新 Xamarin.iOS 应用程序 | 微软文档

将后台更新推送到您的应用程序 | 苹果开发者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM