如何使用目标C实现LocalNotification? [英] How to implement LocalNotification using objective C?

查看:102
本文介绍了如何使用目标C实现LocalNotification?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将本地通知实施到我的应用程序中。我不知道如何正确操作,下面是我用于新数据到达流程的代码,此后如何实现通知流程,我需要通知在<$ c $期间c>前景和背景这两个时间

I am trying to implement local notification into my application. I don't know how to do properly, below code i am using for new data arrival process, here after how to implement Notification process and I need notification during foreground and background both time

下面我已成功后台获取新数据到达检查方法的过程

Below I had successfully background fetching process for new data arrival checking method

 //  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:

上面的代码最终给出了一些字符串 value ...一旦价值来了,我想要显示通知。我正在做的所有事情 appdelegate

Above code finally giving some string value...Once the value came I want to show notification. Everything I am doing into appdelegate.

谢谢

推荐答案

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

现在让我们自己创建通知

Now let’s create the notification itself

    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;

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

Once we have the notification created we need to schedule it with the app.

// 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];

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

If we leave things the way they are now a notification will only appear on screen if the app is in the background. In order to display something when the app is in the foreground and a notification fires we need to implement a method in the app 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];
}

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

We added a icon badge to our app, and this icon badge will only display when the app is in the background. Generally you want to dismiss the icon once a user has opened the app and seen the notification. We’ll need to handle this in the app delegate as well.

这两种方法都会处理它。

These two methods will take care of it.

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

这篇关于如何使用目标C实现LocalNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆