Skip to content

Instantly share code, notes, and snippets.

@Krumelur
Created February 24, 2016 10:58
Show Gist options
  • Save Krumelur/1c90319f273d0a7798c1 to your computer and use it in GitHub Desktop.
Save Krumelur/1c90319f273d0a7798c1 to your computer and use it in GitHub Desktop.
using Foundation;
using UIKit;
using System;
namespace NotifyMe
{
public static class NotificationHelper
{
public static void CreateDailyNotification (string title, string message, DateTime shownFirst)
{
// Code for iOS8+!
var app = UIApplication.SharedApplication;
// Configure required settings - user has to agree.
if (((app.CurrentUserNotificationSettings.Types & UIUserNotificationType.Alert) != UIUserNotificationType.Alert)
|| ((app.CurrentUserNotificationSettings.Types & UIUserNotificationType.Badge) != UIUserNotificationType.Badge)
|| ((app.CurrentUserNotificationSettings.Types & UIUserNotificationType.Sound) != UIUserNotificationType.Sound))
{
var settings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert
| UIUserNotificationType.Badge
| UIUserNotificationType.Sound, new NSSet ());
app.RegisterUserNotificationSettings (settings);
Console.WriteLine ("User has to agree to notifications first!");
return;
}
// Will be shown as a badge while app is running or as an alert if configured as such by the user in the settings app
// and only if the app is not in the foreground.
var notif = new UILocalNotification {
AlertBody = message,
AlertTitle = title,
FireDate = (NSDate)shownFirst,
RepeatInterval = NSCalendarUnit.Day
};
app.ScheduleLocalNotification (notif);
}
}
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to application events from iOS.
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override void DidRegisterUserNotificationSettings (UIApplication application, UIUserNotificationSettings notificationSettings)
{
Console.WriteLine ("User accepted notifications!");
}
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
NotificationHelper.CreateDailyNotification ("Hi there!", "Time to remind you!", DateTime.Now.AddSeconds (20));
return true;
}
public override void OnResignActivation (UIApplication application)
{
// Invoked when the application is about to move from active to inactive state.
// This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message)
// or when the user quits the application and it begins the transition to the background state.
// Games should use this method to pause the game.
}
public override void DidEnterBackground (UIApplication application)
{
// Use this method to release shared resources, save user data, invalidate timers and store the application state.
// If your application supports background exection this method is called instead of WillTerminate when the user quits.
}
public override void WillEnterForeground (UIApplication application)
{
// Called as part of the transiton from background to active state.
// Here you can undo many of the changes made on entering the background.
}
public override void OnActivated (UIApplication application)
{
// Restart any tasks that were paused (or not yet started) while the application was inactive.
// If the application was previously in the background, optionally refresh the user interface.
}
public override void WillTerminate (UIApplication application)
{
// Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment