Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xamarin ios中未第二次触发本地通知_Ios_Xamarin_Xamarin.forms - Fatal编程技术网

在xamarin ios中未第二次触发本地通知

在xamarin ios中未第二次触发本地通知,ios,xamarin,xamarin.forms,Ios,Xamarin,Xamarin.forms,我正在使用消息中心发送通知。第一个很好用。但第二个没有开火 MessagingCenter.Subscribe<string>(this, "iOSNotification", (value) => { NotificationDelegate.RegisterNotification("Trip started"); }); MessagingCenter.Subscribe<str

我正在使用消息中心发送通知。第一个很好用。但第二个没有开火

    MessagingCenter.Subscribe<string>(this, "iOSNotification", (value) =>
    {
        NotificationDelegate.RegisterNotification("Trip started");
    });
    MessagingCenter.Subscribe<string>(this, "IosNotMoved", (value) =>
    {
        NotificationDelegate.RegisterNotification("Would you like to stop trip?");
    });

public static void RegisterNotification(string notify)
{
UILocalNotification notification = new UILocalNotification();
notification.FireDate = NSDate.FromTimeIntervalSinceNow(1);
notification.AlertAction = "View Alert";
notification.AlertBody = notify;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
MessagingCenter.Subscribe(此“iOSNotification”(值)=>
{
NotificationDelegate.RegisterNotification(“Trip started”);
});
MessagingCenter.Subscribe(此“IosNotMoved”(值)=>
{
NotificationDelegate.RegisterNotification(“您想停止旅行吗?”);
});
公共静态无效注册表通知(字符串通知)
{
UILocalNotification通知=新建UILocalNotification();
notification.FireDate=NSDate.FromTimeIntervalSinceNow(1);
notification.AlertAction=“查看警报”;
notification.AlertBody=通知;
UIApplication.SharedApplication.ScheduleLocalNotification(通知);
}

应用程序启动后,应通过将以下代码添加到
AppDelegate
FinishedLaunching
方法并设置所需的通知类型(
未授权选项
)来请求通知权限:

iOS 10的新增功能是,当应用程序位于前台并触发通知时,它可以以不同的方式处理通知。通过提供一个
UNUserNotificationCenterDelegate
并实现
UserNotificationCentermethod
,应用程序可以接管显示通知的责任。例如:

using System;
using ObjCRuntime;
using UserNotifications;


namespace xxx
{
 public class NotificationDelegate:UNUserNotificationCenterDelegate
   {
    public NotificationDelegate()
    {
    }

    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do something with the notification
        Console.WriteLine("Active Notification: {0}", notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
    }


    public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        // Take action based on Action ID
        switch (response.ActionIdentifier)
        {
            case "reply":
                // Do something
                break;
            default:
                // Take action based on identifier
                if (response.IsDefaultAction)
                {
                    // Handle default action...
                }
                else if (response.IsDismissAction)
                {
                    // Handle dismiss action
                }
                break;
        }

        // Inform caller it has been handled
        completionHandler();
    }

  }
}
调用此方法时,例如emample:

RegisterNotification(20);//set the time you want to push notification
通知将在20秒后推送,如果您关闭应用程序,则取消

您可以访问该链接以了解更多信息和详细信息:

 public void RegisterNotification(long time)
    {
        UNUserNotificationCenter center = UNUserNotificationCenter.Current;

        //creat a UNMutableNotificationContent which contains your notification content
        UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();

        notificationContent.Title = "xxx";
        notificationContent.Body= "xxxx";

        notificationContent.Sound = UNNotificationSound.Default;

        UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(time, false);

        UNNotificationRequest request = UNNotificationRequest.FromIdentifier("FiveSecond", notificationContent, trigger);


        center.AddNotificationRequest(request,(NSError obj) => 
        {
           


        });

    }
RegisterNotification(20);//set the time you want to push notification