如何使用 Objective-C 实现 LocalNotification?

How to implement LocalNotification using Objective-C?

我正在尝试在我的应用程序中实现本地通知。我不知道如何正确地做,下面是我用于新数据到达过程的代码,这里是在如何实现通知过程之后,我需要在 foregroundbackground 模式下进行通知。

下面我已经成功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];

如果我们保持现状,只有应用程序在后台时,通知才会出现在屏幕上。为了在应用程序位于前台并触发通知时显示某些内容,我们需要在应用程序委托中实现一个方法。

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

我们为我们的应用程序添加了一个图标徽章,这个图标徽章只会在应用程序处于后台时显示。通常,您希望在用户打开应用并看到通知后关闭该图标。我们也需要在应用委托中处理这个问题。

这两个方法就搞定了。

- (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 10 苹果文档:

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
            arguments:nil];
content.sound = [UNNotificationSound defaultSound];
 
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
            triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
            content:content trigger:trigger];
 
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:nil];