Skip to content

Instantly share code, notes, and snippets.

@CH3COOH
Created January 5, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CH3COOH/2d6f0dadaa929de6788387b0af9be14d to your computer and use it in GitHub Desktop.
Save CH3COOH/2d6f0dadaa929de6788387b0af9be14d to your computer and use it in GitHub Desktop.
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