Last active
August 29, 2015 14:00
-
-
Save hanxue/11249728 to your computer and use it in GitHub Desktop.
Example Mac Notification code
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
#import <Cocoa/Cocoa.h> | |
@interface AppDelegate : NSObject <NSApplicationDelegate> { | |
@private | |
NSWindow *window; | |
} | |
@property (assign) IBOutlet NSWindow *window; | |
@end |
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
#include <AudioToolbox/AudioToolbox.h> | |
#import "AppDelegate.h" | |
@implementation AppDelegate | |
@synthesize window; | |
- (void)downloadDataFromProvider | |
{ | |
// Connect to provider and download waiting data | |
// Check in with provider when first launch and again when | |
// a push notification is received | |
} | |
- (void)sendToProvider:(NSData *)message | |
{ | |
// Connect to provider and send to deviceToken | |
} | |
static void soundCompleted(SystemSoundID soundFileObject, void *clientData) | |
{ | |
// Clean up | |
if(soundFileObject != kSystemSoundID_UserPreferredAlert) { | |
AudioServicesDisposeSystemSoundID(soundFileObject); | |
} | |
} | |
- (void)playNotificationSound:(NSDictionary *)apsDictionary | |
{ | |
// Implement preference sot aht user could specify if they want sounds/alerts | |
// if(userEnabledSounds) | |
NSString *soundName = (NSString *)[apsDictionary valueForKey:(id)@"sound"]; | |
if(soundName != nil) { | |
SystemSoundID soundFileObject = kSystemSoundID_UserPreferredAlert; | |
CFURLRef soundFileURLRef = NULL; | |
if([soundname compare:@"default"] != NSOrderedSame) { | |
CFBundleRef mainBundle = CFBundleGetMainBundle(); | |
// get url to sound file to play | |
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, | |
(CFStringRef) soundName, | |
NULL, | |
NULL); | |
//Create a system sound object representing the file | |
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject); | |
CFRelease(soundFileURLRef); | |
} | |
// Register a function to call when sound is done playing | |
AudioServicesAddSystemSoundCompletion(soundFileObject, NULL, NULL, soundCompleted, NULL); | |
// Play the sound | |
AudioServicesPlaySystemSound(soundFileObject); | |
} | |
} | |
- (void)badgeApplicationIcon:(NSDictinoary *)apsDictionary | |
{ | |
id badge = [apsDictionary valueForKey:@"badge"]; | |
NSDockTile *dockTile = [NSApp dockTile]; | |
if(badge != nil) { | |
NSString *label = [NSSrting stringWithFormat:@"%@", badge]; | |
[dockTile setBadgeLabel: label]; | |
} | |
else { | |
[dockTile setBadgeLabel:nil]; | |
} | |
} | |
- (void)showNotificationAlert:(NSDictionary *) apsDictionary | |
{ | |
// Implement pref whether user want sound/alerts | |
// if(userEnabledAlerts) | |
// Only handle the simple case of alert property contaning string value | |
NSString *message = (NSString *)[apsDictionary valueForKey:(id)@"alert"]; | |
NSAlert *alert = [[NSAlert alloc] init]; | |
[alert addButtonWithTitle:@"OK"]; | |
[alert setMessageText:message]; | |
[alert setAlertStyle:NSInformationAlertStyle]; | |
if([alert runModal] == NSAlertFirstButtonReturn) { | |
// Processing when OK button is clicked. | |
} | |
[alert release]; | |
} | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
// Code to initialize application | |
NSLog(@"%@", NSStringFromSleector(_cmd)); | |
// Register for push notification | |
[NSApp registerForRemoteNotificationTypes:NSRemoteNotificationTypeBadge]; | |
NSString *name = [aNotification name]; | |
NSLog(@"didFinishLaunchingWithOptions: notification name %@", name); | |
// Contact the provider and download the latest data | |
[self downloadDataFromProvider]; | |
// items downloaded, reset icon badge. Assumes badge is showing number | |
// if new items not yet downloaded | |
// If you are not using badge to show number of unread items, don't reset badge here | |
[[NSApp dockTile] setBadgeLabel:nil]; | |
} | |
- (void)application:(NSApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error | |
{ | |
NSLog(@"%@ with error = %@", NSStringFromSelector(_cmd), error); | |
} | |
- (void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo | |
{ | |
NSLog(@"%@", NSStringFromSelector(_cmd)); | |
NSDictionary *apsDictionary = [userInfo valueForKey:@"aps"]; | |
if(apsDictionary != nil) { | |
// A notification arrived while the app is frontmost. | |
// play the sound | |
[self playNotificationSound:apsDictionary]; | |
// Badge the icon | |
[self badgeApplicationIcon:apsDictionary]; | |
// Show the alert | |
[self showNotificationAlert:apsDictionary]; | |
// Get updated content from provider | |
[self downloadDataFromProvider]; | |
} | |
} | |
@end |
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
#import <Cocoa/Cocoa.h> | |
int main(int argc, char *argv[]) | |
{ | |
return NSApplicationMain(argc, (const char **)argv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment