1

I am making an app in which user select time and video. When notification fire than I want that the select video should be played . How can i achieve that? Here is my notification code.

-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    localNotif.fireDate = fireDate;
    localNotif.timeZone = [NSTimeZone localTimeZone];
    localNotif.alertBody = @"Time to wake Up";
    localNotif.alertAction = @"Show me";
    localNotif.soundName = @"Tick-tock-sound.mp3";
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.repeatInterval = NSCalendarUnitDay;
    NSLog(@" date %lu",kCFCalendarUnitDay);
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
}

Any Syggestion?

2 Answers2

1

Your code for scheduling a local notification looks good. Probably you should also add some data in the userInfo property of the local notification, so that when it fires, you can check that property and do something different (play a specific video) according to the data inside the userInfo.

Example:

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"video1" forKey:@"videoName"];
localNotif.userInfo = infoDict;

Make sure that you also request user permission for using local notifications, otherwise the local notification will not fire.

Example:

UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

Now you need to handle the firing of the local notification when your app is in the 3 states: foreground, background/suspended and not running.

The app is running in the foreground. The local notification fires at the date you set it to. The following delegate method will get called by the system in AppDelegate:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSString *videoName = [notification.userInfo objectForKey:@"videoName"];
    //do something, probably play your specific video
}

The app is running in the background or is suspended. The local notification is shown to the user (it fired at the date you set it to) and the user taps on it. The same delegate method as above (didReceiveLocalNotification) will get called by the system in AppDelegate:

The app is not running, the local notification is shown to the user (it fired at the date you set it to) and the user taps on it. The following delegate method will get called by the system in AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif)
    {
       //the app was launched by tapping on a local notification
       NSString *videoName = [localNotif.userInfo objectForKey:@"videoName"];
       // play your specific video
    } else {
       // the app wasn't launched by tapping on a local notification
       // do your regular stuff here
    }
}

I would recommend reading Apple's documentation regarding working with local notifications.

You can use the Media Player framework as recommended in Glorfindel's answer and you can find an example for playing a video in this StackOverflow answer.

Community
  • 1
  • 1
Alex
  • 2,325
  • 3
  • 29
  • 35
0

Once the user opens your local notification, your app will be launched, and the - application:didFinishLaunchingWithOptions: of your UIApplicationDelegate will be called. The options dictionary will contain a UIApplicationLaunchOptionsLocalNotificationKey key, which contains the UILocalNotification. That should give you enough information to determine which video needs to be played, which you can do with the Media Player framework.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109