您安排本地通知的代码看起来不错。可能您还应该在本地通知的userInfo 属性中添加一些数据,以便当它触发时,您可以检查该属性并根据userInfo 中的数据执行不同的操作(播放特定视频)。
例子:
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"video1" forKey:@"videoName"];
localNotif.userInfo = infoDict;
确保您还请求用户许可才能使用本地通知,否则本地通知不会触发。
例子:
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
现在您需要在应用处于 3 种状态时处理本地通知的触发:前台、后台/挂起和未运行。
应用正在前台运行。本地通知在您设置的日期触发。系统会在AppDelegate 中调用以下委托方法:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSString *videoName = [notification.userInfo objectForKey:@"videoName"];
//do something, probably play your specific video
}
应用正在后台运行或被暂停。本地通知会显示给用户(它在您设置的日期触发)并且用户点击它。与上述相同的委托方法(didReceiveLocalNotification)将在AppDelegate中被系统调用:
应用没有运行,本地通知显示给用户(它在您设置的日期触发)用户点击它。系统将在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
}
}
我建议阅读 Apple's documentation 关于处理本地通知的内容。
您可以使用Glorfindel 的回答中推荐的媒体播放器框架,您可以在此StackOverflow answer 中找到播放视频的示例。