如何使用目标C实现LocalNotification?

Ans*_*rMe 6 notifications objective-c ios

我正在尝试实施local notification我的application.我不知道怎么做正确,下面的代码我用于新的数据到达过程,这里是如何实现通知过程,我需要notification在期间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:
Run Code Online (Sandbox Code Playgroud)

上面的代码终于给出了一些string价值......一旦价值来了,我想show notification.我正在做的一切appdelegate.

谢谢

Meh*_*hul 14

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];
}
Run Code Online (Sandbox Code Playgroud)

2)如果应用程序已打开(在本地通知触发之前),请取消本地通知

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
Run Code Online (Sandbox Code Playgroud)

//用于本地通知

我们需要做的第一件事就是注册通知.

 // New for iOS 8 - Register the notifications
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
Run Code Online (Sandbox Code Playgroud)

现在让我们自己创建通知

    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;
Run Code Online (Sandbox Code Playgroud)

创建通知后,我们需要使用应用程序安排通知.

// 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];
Run Code Online (Sandbox Code Playgroud)

如果我们按现在的方式保留通知,则只有在应用程序位于后台时,通知才会显示在屏幕上.为了在应用程序位于前台并显示通知时显示某些内容,我们需要在app委托中实现一个方法.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

这两种方法都会照顾它.

- (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;
}
Run Code Online (Sandbox Code Playgroud)


Ofi*_*chi 6

苹果文档的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 completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)