在 iOS 中为 Xamarin.Forms 运行 App 时未收到推送通知 [英] Not getting push notification when App is running in iOS for Xamarin.Forms

查看:28
本文介绍了在 iOS 中为 Xamarin.Forms 运行 App 时未收到推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 UrbanAirship 的推送通知.当应用程序在后台关闭时,我会收到通知.但是我在应用运行时没有收到通知.

I have implemented UrbanAirship for push notification. I get notification when app is closed of in background. But I am not getting notification when app is running.

我附上了我的代码.任何人都可以查看并告诉我我在这里缺少什么吗?

I have attached my code. Can anybody please look into and let me know what I am missing here?

谢谢.

AppDelegates.cs

AppDelegates.cs

public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
    UAirship.TakeOff();

    Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    UIApplication.SharedApplication.StatusBarHidden = false;
    UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);

    _networkManager = new NetworkManager();

    UAirship.Push.UserPushNotificationsEnabled = true; 
    string chanelid = UAirship.Push.ChannelID;

    return base.FinishedLaunching(uiApplication, launchOptions);
}

[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UserNotifications.UNUserNotificationCenter center, UserNotifications.UNNotificationResponse response, Action completionHandler)
{
    var alert = new UIAlertView("Title1", "Message1", null, "Cancel", "OK");
    alert.Show();
    //TAPPED NOTIFICATION
}

//Or 

//Fire when background received notification is clicked
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    var alert = new UIAlertView("Title2", "Message2", null, "Cancel", "OK");
    alert.Show();
}

信息.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>CFBundleExecutable</key>
    <string></string>
    <key>CFBundleDisplayName</key>
    <string>MyProject</string>
    <key>CFBundleName</key>
    <string>MyProject</string>
    <key>CFBundleIdentifier</key>
    <string>com.xxxxxxxx.xxxxx</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MinimumOSVersion</key>
    <string>8.0</string>
    <key>UIDeviceFamily</key>
    <array>
      <integer>1</integer>
      <integer>2</integer>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
      <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>XSAppIconAssets</key>
    <string>Assets.xcassets/AppIcons.appiconset</string>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSExceptionDomains</key>
      <dict>
        //some values
      </dict>
    </dict>
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fb-messenger-api</string>
      <string>fbauth2</string>
      <string>fbshareextension</string>
    </array>
    <key>UIStatusBarHidden</key>
    <true/>
    <key>UIMainStoryboardFile</key>
    <string>LaunchScreen</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string></string>
    <key>UIBackgroundModes</key>
    <array>
      <string>remote-notification</string>
    </array>
  </dict>
</plist>

规格

Windows 10

Visual Studio 2017(企业版)

Visual Studio 2017(Enterprise edition)

Xamarin.Forms

Xamarin.Forms

推荐答案

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

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.

这篇关于在 iOS 中为 Xamarin.Forms 运行 App 时未收到推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆