Skip to content

Instantly share code, notes, and snippets.

@kwylez
Last active March 2, 2020 06:09
Show Gist options
  • Save kwylez/5337918 to your computer and use it in GitHub Desktop.
Save kwylez/5337918 to your computer and use it in GitHub Desktop.
Example implementation of listening for iTunes/Spotify track changes and displaying information in the NotificationCenter. Article reference: http://blog.corywiles.com/now-playing-with-spotify-and-itunes
//
// AppDelegate.m
//
//
// Created by Cory D. Wiles on 10/8/12.
// Copyright (c) 2013 Cory D. Wiles. All rights reserved.
//
#import "AppDelegate.h"
#import "iTunes.h"
#import "SpotifyClient.h"
@interface AppDelegate()
- (void)updateTrackInfoFromITunes:(NSNotification *)notification;
- (void)updateTrackInfoFromSpotify:(NSNotification *)notification;
@end
@implementation AppDelegate
- (void)dealloc {
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc removeObserver:self name:@"com.apple.iTunes.playerInfo" object:nil];
[dnc removeObserver:self name:@"com.spotify.client.PlaybackStateChanged" object:nil];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.masterViewController = [[WNMasterViewController alloc] initWithNibName:@"WNMasterViewController" bundle:nil];
[self.window.contentView addSubview:self.masterViewController.view];
self.masterViewController.view.frame = ((NSView*)self.window.contentView).bounds;
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
SpotifyClientApplication *spotify = [SBApplication applicationWithBundleIdentifier:@"com.spotify.client"];
if ([iTunes isRunning]) {
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(updateTrackInfoFromITunes:)
name:@"com.apple.iTunes.playerInfo"
object:nil];
} else if (spotify.isRunning) {
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(updateTrackInfoFromSpotify:)
name:@"com.spotify.client.PlaybackStateChanged"
object:nil];
} else {
[[NSAlert alertWithMessageText:NSLocalizedString(@"iTunes nor Spotify are Running", nil)
defaultButton:NSLocalizedString(@"OK", nil)
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"iTunes nor Spotify are currently running, thus songs won't be sync'd"] runModal];
}
[self.window makeKeyAndOrderFront:self];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)application
hasVisibleWindows:(BOOL)flag {
[self.window makeKeyAndOrderFront:self];
return YES;
}
#pragma mark - Methods
- (void)updateTrackInfoFromITunes:(NSNotification *)notification {
NSLog(@"notification payload: %@", notification.userInfo);
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
if ([iTunes isRunning]) {
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:[[[iTunes currentTrack] get] name]];
[notification setInformativeText:[[[iTunes currentTrack] get] artist]];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]]];
[notification setSoundName:NSUserNotificationDefaultSoundName];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
}
}
- (void)updateTrackInfoFromSpotify:(NSNotification *)notification {
SpotifyClientApplication *spotify = [SBApplication applicationWithBundleIdentifier:@"com.spotify.client"];
SpotifyClientTrack *spotifyTrack = [spotify currentTrack];
NSLog(@"notification payload: %@", notification.userInfo);
if ([[notification.userInfo valueForKey:@"Player State"] isEqualToString:@"Stopped"]) {
return;
}
/**
* If you don't have a paid account then you'll get adverts that register as
* 'songs'. So you'll probably want to skip the notification in a real app.
*/
if ([[spotifyTrack album] hasPrefix:@"http"]) {
NSLog(@"Album prefix = http (%@)", [spotifyTrack album]);
}
NSUserNotification *notif = [[NSUserNotification alloc] init];
[notif setTitle:[spotifyTrack name]];
[notif setInformativeText:[spotifyTrack albumArtist]];
[notif setDeliveryDate:[NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]]];
[notif setSoundName:NSUserNotificationDefaultSoundName];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notif];
}
@end
@JeMiMe83
Copy link

Hi there, I made some iOS app and I would like to sent notifications to those app when a new app is available.
What do I have to do to be able to send such notifications from my Mac ?
Thanks in advance for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment