I'm working to get osx native notifications working. I'm able to create a notification but not get the didActivateNotification working. I want didActivateNotification to allow me to focus the window and remove the notification from the notification center.
Here is my code: notice.m
#import "Notice.h"
#implementation Notice
- (void) notify:(NSDictionary *)message {
NSLog(#"Notification - Show it");
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:[message valueForKey:#"title"]];
[notification setInformativeText:[message valueForKey:#"content"]];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]];
[notification setSoundName:NSUserNotificationDefaultSoundName];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSLog(#"Notification - Clicked");
notification=nil;
[center removeDeliveredNotification: notification];
}
This is firing properly:
NSLog(#"Notification - Show it");
But this is not:
NSLog(#"Notification - Clicked");
Any suggestions? Thanks
You probably have to set the delegate for NSUserNotificationCenter:
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;
You should also make sure your Notice class implements the NSUserNotificationCenterDelegate protocol.
Related
Since MacOS 10.13 everytime I click the close button on a NSUserNotification it calls:
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
How can I prevent this or handle the close vs the action button
To create the notification I do:
NSUserNotification *notification = [[NSUserNotification alloc] init];
...
[notification setHasActionButton:false];
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:(id)self];
and NSUserNotificationAlertStyle in the .plist is set to "alert"
but now basically the close button reacts the same way the actionButton does??
NSUserNotification has property from which you can manage notification identifier or hasActionButton value, so you can handle the close vs the action button with if else in the same delegate method
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification{
}
This is works for me..
you can remove notification in didActivateNotification: method
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSLog(#"Notification - Clicked");
[center removeDeliveredNotification: notification];
notification=nil;
}
where center is...
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
I've to develop a tiny program that send a local notification in on my Mac. This notification must be sent every 30 sec, so I create a command line tool with Xcode and I choose to develop in Objective-C. Xcode shows me the main.m file, in which I wrote the following code:
main.m
#import <Foundation/Foundation.h>
#import "Notification.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
Notification *notification = [[Notification alloc]init];
[notification startNotification];
}
return 0;
}
where Notification it's a class in which I start a timer and I send the notification, the code is:
Notification.h
#interface Notification : NSObject <NSUserNotificationCenterDelegate>
- (void) startNotification;
#end
Notification.m
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "Notification.h"
#implementation Notification : NSObject
- (void) startNotification {
NSTimer *timer = [[NSTimer alloc]init];
if (timer == nil) {
timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(sendNotification) userInfo:nil repeats:YES];
}
}
- (void)sendNotification {
NSUserNotification *notification = [[NSUserNotification alloc]init];
notification.title = #"Prova";
notification.informativeText = #"Ciao Salame";
notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
[center setDelegate:self];
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:#"OK"];
[alert setMessageText:notification.title];
[alert setInformativeText:notification.informativeText];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
[center removeDeliveredNotification:notification];
}
#end
when I run the app it doesn't calls the selector method and it finish to run, what's wrong in my little program? Can you help me to fix it?
Thank you
AppleScript
I create a tiny AppleScript and it's working quite well, the code I used is the following:
on idle
display notification "Lorem ipsum dolor sit amet" with title "Title"
return 30
end idle
then I created an app from this script and it will be execute at the startup of the system and it's sending me a notification every 30 sec. I'm not sure if it's the right way to do that, but it's working...
LOOK FROM HERE
Objective-C
I've try to update my code as follow:
Notification.m
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "Notification.h"
#implementation Notification : NSObject
- (void) startNotification {
NSUserNotification *notification = [[NSUserNotification alloc]init];
notification.title = #"Prova";
notification.informativeText = #"Ciao Salame";
notification.soundName = NSUserNotificationDefaultSoundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
[center setDelegate:self];
sleep(30);
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:#"OK"];
[alert setMessageText:notification.title];
[alert setInformativeText:notification.informativeText];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
[center removeDeliveredNotification:notification];
}
but it doesn't send me the notification...
The reason is, that your app directly returns after instantiating Notification. Just calling startNotification does not prevent the app from returning. Probably you want to let it run inside a thread in the background.
I'm using NSUserNotification to display notifications. This is working fine. The problem is that when you click on a notification:
The apps notifications are not removed from the notification center.
The app (when minimized) does not open.
Anyone familiar with the NSUserNotification who can offer some pointers?
notice.m
#import "Notice.h"
#implementation Notice
- (void) notify:(NSDictionary *)message {
NSLog(#"Notification - Show it");
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:[message valueForKey:#"title"]];
[notification setInformativeText:[message valueForKey:#"content"]];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]];
[notification setSoundName:NSUserNotificationDefaultSoundName];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSLog(#"Notification - Clicked");
notification=nil;
[center removeDeliveredNotification: notification];
}
#pragma mark WebScripting Protocol
+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
if (selector == #selector(notify:))
return NO;
return YES;
}
+ (NSString*) webScriptNameForSelector:(SEL)selector
{
id result = nil;
if (selector == #selector(notify:)) {
result = #"notify";
}
return result;
}
// right now exclude all properties (eg keys)
+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
return YES;
}
#end
Thank you
Just implement the NSUserNotificationCenterDelegate and define this method:
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
Example:
This is what I did in a "notifier" application.
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSRunAlertPanel([notification title], [notification informativeText], #"Ok", nil, nil);
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
notifications=nil;
[tableView reloadData];
[center removeDeliveredNotification: notification];
}
When the notification is activated (click by the user) I just inform the user with a panel (I could use a hud window).In this case I immediately remove the delivered notification, but this is not what happens usually.The notification could stay there some time and be removed after 1/2 hours (it depends on the application that you are developing).
I have a MPMoviePlayerController subclass that should show the controls when playback is finished. I have attached a responder to the MPMoviePlayerPlaybackDidFinishNotification notification and tried setting the control style as follows:
[self setControlStyle:MPMovieControlStyleEmbedded];
This is not working. In essence, at the end of the video I want to controls to show.
How can I show controls programatically?
NOTE: The controller is NOT in fullscreen mode.
Kindly find my full Code about this , it's working with me
add .h class add this
#property(strong,nonatomic) MPMoviePlayerViewController * moviePlayer;
at .m class add this code "pass the movie URl"
-(void) playMovie:(NSString *)filePath
{
NSURL *theOutputURL = [NSURL fileURLWithPath:filePath];
if(_moviePlayer)
[_moviePlayer.moviePlayer setContentURL:theOutputURL];
else
_moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:theOutputURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_moviePlayer.moviePlayer];
if (![_moviePlayer.moviePlayer isPreparedToPlay])
[_moviePlayer.moviePlayer prepareToPlay];
_moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[_moviePlayer.moviePlayer setFullscreen:YES];
_moviePlayer.moviePlayer.controlStyle=MPMovieControlStyleEmbedded;
[_moviePlayer.moviePlayer setContentURL:theOutputURL];
_moviePlayer.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height);
[_moviePlayer shouldAutorotateToInterfaceOrientation: AVCaptureVideoOrientationLandscapeRight];
[self.view addSubview:_moviePlayer.view];
}
- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification {
if (_moviePlayer.moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
[_moviePlayer.moviePlayer setContentURL:[_moviePlayer.moviePlayer contentURL]];
[_moviePlayer.moviePlayer play];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
// to add your code after playback is finished
}
I'm trying to use the MPMoviePlayerController class on the iPad.
Here's my code:
multimediaPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
multimediaPlayer.movieControlMode = MPMovieControlModeDefault;
[multimediaPlayer play];
and this works very well on the iPhone but it don't want to run on the iPad. I hear the sound of the video, but the movie doesn't playing.
Why it can be this problem?
Below code working perfect for my application. Hope it would do same for you.
The main thing is to set the frame of mpMoviePlayerController's frame. if you don't do it, it would almost not show the video.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Register to receive a notification when the movie scaling mode has changed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:nil];
kDomain = [NSString stringWithString:#"http://www.virtua-book.com/"];
[navigationController setNavigationBarHidden:YES];
NSURL *ur=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IPAD" ofType:#"mp4"]];
mpMCtr=[[MPMoviePlayerController alloc] initWithContentURL:ur];
mpMCtr.fullscreen=YES;
[mpMCtr setScalingMode:MPMovieScalingModeFill];
[mpMCtr setShouldAutoplay:YES];
[mpMCtr setControlStyle:MPMovieControlStyleNone];
[mpMCtr setMovieSourceType:MPMovieSourceTypeFile];
mpMCtr.view.frame = CGRectMake(0, 0, 1024, 768);
[mpMCtr setRepeatMode:MPMovieRepeatModeNone];
[mpMCtr play];
[ur release];
// Override point for customization after app launch
[navigationController.view addSubview:mpMCtr.view];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
// Notification called when the movie finished playing.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[mpMCtr.view removeFromSuperview];
}
To fix back/forward (or previous/next) buttons you should do the following:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
...
- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification {
if (moviePlayer.playbackState == MPMoviePlaybackStateStopped) {
[moviePlayer setContentURL:[moviePlayer contentURL]];
[moviePlayer play];
}
}
Something along these lines is probably what you want to do:
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentUrl:movieUrl];
[self presentMoviePlayerViewController:mpvc];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
mp.moviePlayer.controlStyle = 2;
Ok, guys, I found that this: is deprecated.
The solution is multimediaPlayer.controlStyle = MPMovieControlStyleDefault; but it still doesn't work.