Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin.Macでローカル通知をおこなう
using System;
using System.Collections.Generic;
using AppKit;
using Foundation;
namespace LocalNotification
{
public partial class ViewController : NSViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
// Update the view, if already loaded.
}
}
partial void buttonAction(NSObject sender)
{
var dc = NSUserNotificationCenter.DefaultUserNotificationCenter;
dc.Delegate = new UserNotificationCenterDelegate();
var keys = new [] { "url" };
var values = new object[] { "http://blog.ch3cooh.jp/" };
var userInfo = NSDictionary.FromObjectsAndKeys(values, keys);
var notification = new NSUserNotification();
notification.Title = "タイトル";
notification.Subtitle = "サブタイトル";
notification.InformativeText = "本文";
notification.UserInfo = userInfo;
dc.DeliverNotification(notification);
}
class UserNotificationCenterDelegate : NSUserNotificationCenterDelegate
{
public override void DidActivateNotification(NSUserNotificationCenter center, NSUserNotification notification)
{
var urlString = notification.UserInfo?["url"] as NSString;
if (urlString != null)
{
var url = new NSUrl(urlString);
NSWorkspace.SharedWorkspace.OpenUrl(url);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.