Не появляется push-уведомление, когда приложение работает в iOS для Xamarin.Forms - PullRequest
0 голосов
/ 09 мая 2018

Я реализовал UrbanAirship для push-уведомлений.Я получаю уведомление, когда приложение закрыто в фоновом режиме.Но я не получаю уведомления, когда приложение работает.

Я прикрепил свой код.Кто-нибудь, пожалуйста, посмотрите и дайте мне знать, что мне здесь не хватает?

Спасибо.

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 (выпуск Enterprise)

Xamarin.Forms

1 Ответ

0 голосов
/ 09 мая 2018

У нас было много проблем с тем, чтобы заставить нас работать. Вот что мы придумали, что работало на переднем плане и на заднем плане:

[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, но попробуйте этот метод и посмотрите, подходит ли он вам.

Кроме того, я предполагаю, что если вы получаете какие-либо уведомления, тогда ваш сертификат и права установлены правильно; но перепроверьте их на всякий случай.

...