我正在尝试在我的应用程序中实现本地通知。我不知道如何正确地做,下面是我用于新数据到达过程的代码,这里是在如何实现通知过程之后,我需要在 foreground
和 background
模式。
下面我已经成功地为新的数据到达检查方法后台
获取过程
// Value matching and trying to get new data
[live_array removeObjectsInArray:stored_array];
// if you require result as a string
NSString *result = [stored_array componentsJoinedByString:@","];
NSLog(@"New Data: %@", result); // objects as string:
上面的代码最终给出了一些 string
值...一旦值出现,我想显示通知。我所做的一切都在 App Delegate 中。
最佳答案
1) 当应用关闭时,安排在 24 小时内触发的本地通知
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
notification.alertBody = @"24 hours passed since last visit :(";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
2) 如果应用打开(在本地通知触发之前),取消本地通知
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
//用于本地通知
我们需要做的第一件事是注册通知。
// New for iOS 8 - Register the notifications
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
现在让我们自己创建通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = _datePicker.date;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
notification.fireDate = fireTime;
notification.alertBody = @"Alert!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.applicationIconBadgeNumber = 1;
notification.soundName = UILocalNotificationDefaultSoundName;
switch (_frequencySegmentedControl.selectedSegmentIndex) {
case 0:
notification.repeatInterval = NSCalendarUnitDay;
break;
case 1:
notification.repeatInterval = NSCalendarUnitWeekOfYear;
break;
case 2:
notification.repeatInterval = NSCalendarUnitYear;
break;
default:
notification.repeatInterval = 0;
break;
}
notification.alertBody = _customMessage.text;
创建通知后,我们需要使用应用安排它。
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
// this will fire the notification right away, it will still also fire at the date we set
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
如果我们让事情保持现状,只有当应用程序在后台时,通知才会出现在屏幕上。为了在应用程序位于前台并触发通知时显示某些内容,我们需要在应用程序委托(delegate)中实现一个方法。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification Received" message:notification.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
我们在我们的应用程序中添加了一个图标角标(Badge),这个图标角标(Badge)只会在应用程序处于后台时显示。通常,您希望在用户打开应用并看到通知后关闭该图标。我们还需要在应用委托(delegate)中处理这个问题。
这两个方法会处理它。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(@"%s", __PRETTY_FUNCTION__);
application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"%s", __PRETTY_FUNCTION__);
application.applicationIconBadgeNumber = 0;
}
关于ios - 如何使用 Objective-C 实现 LocalNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087551/