【问题标题】:Local Push notification not working in Xamarin iOS本地推送通知在 Xamarin iOS 中不起作用
【发布时间】:2020-06-30 12:10:29
【问题描述】:

我正在使用 Xamarin.forms 并为 iOS 实现了本地推送通知。当我通过 Visual Studio 调试应用程序时,即使应用程序被最小化,它也能成功运行,应用程序可以接收通知。但是,在没有通过 Visual Studio 调试的情况下直接运行应用程序时,应用程序无法显示通知。请指导我。

然后我也尝试通过将应用程序发布到应用商店但遇到相同的情况,应用程序即使在前台模式下也无法收到通知。

我已经在 Info.plist 中的 Background Modes 下选择了“Background fetch”属性。 我还在 FinishedLaunching 方法的行下方添加了

UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

整个代码实现如下

   public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        try
        {
            UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

            try
            {                    

                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert |
                        UNAuthorizationOptions.Sound |
                        UNAuthorizationOptions.Sound,
                        (granted, error) =>
                        {
                            if (granted)
                            {
                                InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);                                    
                            }
                        });
                }
                else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                    new NSSet());

                    UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
                else
                {
                    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
                }

                bool IsRegistered = UIApplication.SharedApplication.IsRegisteredForRemoteNotifications;
            }
            catch (Exception ex)
            {
                UIAlertView avAlert = new UIAlertView("FinishedLaunching Push Notification Exception", ex.Message, null, "OK", null);
                avAlert.Show();
            }

            UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

            LoadApplication(new MessengerClient.App());

        }
        catch (Exception ex)
        {
            NativeHelper.SendUnhandledException(ex, NativeHelper.iOS + ": FinishedLaunching");
        }
        return base.FinishedLaunching(app, options);
    }

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {  
        /// reset our badge
        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

        /// Cancel/clear all Local notifications fronm the tray.
        UIApplication.SharedApplication.CancelLocalNotification(notification);

        /// Cancel/clear all notifications fronm the tray.
        UIApplication.SharedApplication.CancelAllLocalNotifications();
    }

显示通知的代码如下。

UILocalNotification notification = new UILocalNotification();
        notification.FireDate = NSDate.FromTimeIntervalSinceNow(1);
        notification.AlertAction = title;
        notification.AlertBody = content;
        notification.AlertTitle = title;
        notification.SoundName = UILocalNotification.DefaultSoundName;
        notification.ApplicationIconBadgeNumber = 1;            
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);

我知道这是重复的问题,但是我尝试了所有解决方法,但对我不起作用。

【问题讨论】:

    标签: xamarin xamarin.forms push-notification xamarin.ios xamarin-studio


    【解决方案1】:

    您可以通过此 Notifications in Xamarin.iOS 进行检查。

    iOS 应用程序以几乎完全相同的方式处理远程和本地通知。当应用程序运行时,会调用AppDelegate类上的ReceivedLocalNotification方法或ReceivedRemoteNotification方法,并将通知信息作为参数传递。

    应用程序可以以不同的方式处理通知。例如,应用程序可能只是显示一个警报来提醒用户某些事件。或者,通知可能用于向用户显示进程已完成的警报,例如将文件同步到服务器。

    以下代码显示了如何处理本地通知并显示警报并将徽章编号重置为零:

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {
        // show an alert
        UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
        okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
    
        Window.RootViewController.PresentViewController(okayAlertController, true, null);
    
        // reset our badge
        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
    }
    

    另外还有a sample可以下载查看。无论在我的本地站点 (iOS 13.3) 中的 Debug 或 Release 模型中,它都有效。

    效果:

    【讨论】:

    • 我已经使用了您建议的链接并相应地设置了代码。我只想在应用程序最小化时设置本地通知。当我通过 Visual Studio 调试应用程序时,它工作正常,就像您在上图中所做的一样(但是我正在使用真实设备),但是当我从系统中拔下设备并直接运行应用程序时,它无法当应用程序处于最小化代码时接收通知。 我想在应用程序最小化或手机被锁定时收到通知。
    • 举个例子,我的应用程序有聊天功能,当应用程序在后台运行并且有人发送消息时,我想通过本地通知(而不是远程通知)通知用户- 因为我的应用程序已经在最小化模式下运行)。
    • @VivekPatel Okey,如果在聊天功能中,我认为您最好使用自定义服务器向设备发送远程通知。这将保证无论设备处于后台还是锁定状态,应用都会收到通知。
    • Jiang,感谢您的建议,我已经知道远程推送通知,但是我的本地通知代码在 Android 平台上运行良好,所以我想为我的 iOS 实现相同的本地通知,但不幸的是,它不适用于 iOS。所以你对我上面解释的问题有什么想法吗?
    • @VivekPatel 我尝试过使用物理设备,但无法重现它。它有效,现在我不知道这个原因:(
    【解决方案2】:

    只需在您的 AppDelegate DidFinishLaunching 中添加此代码,通知就会在后台开始工作。 BackgroundTask- showNotification 有时可能会被取消,对我来说,每当我的仪表板加载时,我想它有多个 API 调用。因此,将其添加到您的 DidEnterBackground 委托中以及使用不同的 taskID,以启动新的后台任务。对我来说效果很好。

    nint taskID = yourTaskID;
    taskID = application.BeginBackgroundTask("showNotification", expirationHandler: ()=> {
         UIApplication.SharedApplication.EndBackgroundTask(taskID);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-10
      • 2021-11-14
      • 2016-12-29
      • 1970-01-01
      • 2021-11-06
      • 2015-06-28
      • 2019-09-22
      相关资源
      最近更新 更多