如何使用目标 C 实现 LocalNotification? [英] How to implement LocalNotification using objective C?
问题描述
我正在尝试将 local notification
实施到我的 application
中.我不知道如何正确操作,下面是我用于新数据到达过程的代码,在如何实现通知过程之后,我在 foreground
和 notification
期间需要notification
代码>背景代码>两次
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
下面我已经成功地background
获取新数据到达检查方法的过程
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:
上面的代码最终给出了一些 string
值...一旦该值出现,我想显示通知
.我在 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];
}
我们为我们的应用添加了一个图标徽章,这个图标徽章只会在应用处于后台时显示.通常,您希望在用户打开应用程序并看到通知后关闭该图标.我们还需要在应用委托中处理这个问题.
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屋!