ios - 通知触发时播放视频

标签 ios objective-c video uilocalnotification

我正在制作一个用户可以在其中选择时间和视频的应用。当通知触发时,我希望播放所选视频。我怎样才能做到这一点? 这是我的通知代码。

-(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]; 
}

有什么建议吗?

最佳答案

您安排本地通知的代码看起来不错。也许你还应该在本地通知的 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 中的系统将调用以下委托(delegate)方法:

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

该应用正在后台运行或已暂停。向用户显示本地通知(它在您设置的日期触发)并且用户点击它。与上面相同的委托(delegate)方法 (didReceiveLocalNotification) 将在 AppDelegate 中被系统调用:

应用未运行,向用户显示本地通知(它在您设置的日期触发)并且用户点击它AppDelegate 中的系统将调用以下委托(delegate)方法:

- (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 中找到播放视频的示例。 .

关于ios - 通知触发时播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502733/

相关文章:

ios - 如何在 objective-c 的AppDelegate中加载NSUserDefaults standardUserDefaults

ios - 异常 "mutating method sent to immutable object"

video - 如何使用 ffmpeg 创建 m4v 视频?

ios - Objective-C ARC block __strong __weak

android - 从单个图像生成一个视频时,ffmpeg视频连接不起作用

javascript - Vine:以编程方式控制播放

iphone - 调用 referenceSizeForFooterInSection 而不调用 reloadData

ios - 以编程方式显示 TTTableHeaderDragRefreshView 的下拉更新消息

iOS estimatedRowHeight 不适用于 plus 设备

ios - 像 iOS 照片应用程序年/月/日选择器一样对 CollectionView 单元格进行动画处理?