本地推送通知在Xamarin

本地推送通知在Xamarin

本文介绍了本地推送通知在Xamarin iOS中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin.forms并为iOS实现了本地推送通知.当我通过Visual Studio调试应用程序时,即使该应用程序已最小化,该应用程序仍能够成功接收通知.但是,当直接运行该应用程序而未通过Visual Studio进行调试时,该应用程序将无法显示通知.请为此指导我.

I am using Xamarin.forms and implemented Local push notification for iOS. It is working successfully when I am debugging the app through visual studio even when the app is minimized, the app can able to receive the notification. But while running the app directly without debugging through visual studio, the app is not able to display the notification. Kindly guide me on this.

然后,我还尝试通过将应用发布到应用商店来尝试,但是经历了同样的事情,即使在前台模式下,应用也无法接收到通知.

Then I also tried by releasing the app to the app store but experienced the same, the app is not able to receive the notification it not even in foreground mode.

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

I already have selected the "Background fetch" property under Background Modes in Info.plist.I have also added below the line in my FinishedLaunching method

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

用于显示通知的代码如下.

code for displaying the notification is as below.

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

我知道这是重复的问题,但是,我尝试了所有解决方法,但对我没有用.

I know this is the repeat question but, I tried all the workaround but didn't work for me.

推荐答案

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

Just add this code in your AppDelegate DidFinishLaunching and notification will start working in the background. BackgroundTask- showNotification may get canceled sometime, for me whenever my dashboard is loaded, I guess as it has multiple API Calls. so add it in your DidEnterBackground Delegate as well with different taskID, to start a new background task. It works fine for me.

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

这篇关于本地推送通知在Xamarin iOS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:43