Created
January 5, 2017 01:36
-
-
Save CH3COOH/2d6f0dadaa929de6788387b0af9be14d to your computer and use it in GitHub Desktop.
Xamarin.Macでローカル通知をおこなう
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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