【问题标题】:Not getting push notification when App is running in iOS for Xamarin.Forms应用程序在 iOS 中为 Xamarin.Forms 运行时未收到推送通知
【发布时间】:2018-05-09 13:55:34
【问题描述】:

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

我已附上我的代码。有人可以调查一下,让我知道我在这里缺少什么吗?

谢谢。

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();
}

info.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(企业版)

Xamarin.Forms

【问题讨论】:

  • 我更新了代码,如果有帮助请标记为答案。

标签: xamarin.forms xamarin.ios apple-push-notifications urbanairship.com


【解决方案1】:

我们的工作遇到了很多问题。这就是我们想出的在前台和后台工作的方法:

[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 版本中发生了变化,但试试这个方法,看看它是否适合你。

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

【讨论】:

  • 这里也一样,我为此付出了很多时间。感谢您的快速回复。证书已正确安装。当一切都关闭或在后台(最小化)时,我会收到通知。我一定会尝试上述解决方案。我只是想确认info.plist 的设置。你能看看我的info.plist吗?特别是这个键&lt;key&gt;UIBackgroundModes&lt;/key&gt;。再次感谢您。
  • 我的 plist 中没有那个键。我也没有发布 iOS 11 的更新(不确定这是否是 11 的东西)。
  • 嗨,我仍然遇到问题。能否请您提供handleNotificationcompletionHandler 的基本代码?所以,我可以试着得到一些线索
  • @Developer completionHandler 是方法传入的Action&lt;T&gt; 参数。那是操作系统的代码,不是我的。如果对您有帮助,我可以发布我的 handleNotification 代码。
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2019-07-24
  • 2020-12-23
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多