且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 iOS 中为 Xamarin.Forms 运行 App 时未收到推送通知

更新时间:2023-02-26 21:09:32

我们在使我们的工作正常工作时遇到了很多问题.这就是我们在前台和后台工作的想法:

We had a lot of issues getting ours to work. This is what we came up with that worked in the foreground and background:

[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
    //FOREGROUND
    handleNotification(notification.Request.Content.UserInfo);
    completionHandler(UNNotificationPresentationOptions.Alert);
}
public void ApplicationReceivedRemoteMessage(RemoteMessage remoteMessage)
{
    //REMOTE iOS 10
    //NOT USED CURRENTLY
    handleNotification(remoteMessage.AppData);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    //BACKGROUND
    handleNotification(userInfo);
    completionHandler(UIBackgroundFetchResult.NewData);
}

private void handleNotification(NSDictionary userInfo)
{
    var payload = userInfo["TheStringIdentifierWhenSendingANotification"];

    var notificationData = JsonConvert.DeserializeObject<NotificationData>(payload.ToString());

    if (notificationData != null)
        MessagingCenter.Send<INotification, NotificationData>(this, Core.Helpers.Constants.Messaging.Notification, notificationData);
}

我看到的主要区别是我们的方法是 willPresentNotification 而你的方法是 didReceiveNotificationResponse.我不确定它是否在以后的 iOS 版本中发生了变化,但试试这个方法,看看它是否适合你.

The main difference that I see is that our method is willPresentNotification and yours is didReceiveNotificationResponse. I'm not sure if it changed in later iOS versions, but try this method and see if that works for you.

另外,我假设如果您收到任何通知,那么您的证书和权利设置正确;但请仔细检查以防万一.

Also, I'm assuming if you're receiving any notifications, then your certificate and entitlements are setup correctly; but double-check those just in case.