自定义UIButton与本地通知

时间:2016-08-11 22:52:14

标签: ios objective-c

我尝试创建自定义按钮,然后安排本地通知。我在这里找到的代码似乎已经过时了。我查看了Apple https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html的轮廓 但我还是很困惑。任何人都可以发布一个简洁明了的例子,包括我放哪些文件的代码? (包括注册)即使只是创建任何本地通知的示例也会有所帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

在AppDelegate.m中,注册应用支持的用户交互类型(从iOS 8.0及更高版本开始)

UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert);

UIUserNotificationSettings *mySettings =
            [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

在按钮点按方法中,您可以执行此操作:

NSDate * fireDate; // The schedule date time.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.alertBody = @"alertText";
localNotification.alertAction = @"buttonTitle";
localNotification.soundName = UILocalNotificationDefaultSoundName;


// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在AppDelegate.m中添加此方法以处理本地通知何时到达(如有必要):

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification{
    // This method gets called when a local notification arrives
}