- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在我的应用程序中实现本地通知。我不知道如何正确地做,下面是我用于新数据到达过程的代码,这里是在如何实现通知过程之后,我需要在 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/
我想使用 UILocalNotifications 作为应用程序的后台计时器。用户可以像远程通知一样禁用此通知吗?如果用户禁用了应用程序的远程通知,本地通知是否也会被禁用? 最佳答案 我最终编写了一个
是否有任何原因导致我的本地通知显示在通知中心但未弹出?有什么设置可以让它弹出吗? 最佳答案 本地通知将与所有其他通知一样显示,作为开发者,您对此无能为力。 通知的默认设置是将其显示为屏幕顶部的横幅,而
我创建了一个应用程序,该应用程序每天通知用户一次。 为此我使用了下面的代码 func application(application: UIApplication, didRegisterUserN
我正在安排计时器并向用户发送有关某些数据的一些本地通知,例如 - 如果附近有商店。 func configureNotification(shop: Shop) { let notificat
这是我的 didEnterRegion 函数代码。问题是通知在进入和退出时触发。如何仅在用户输入位置时触发通知? func locationManager(manager: CLLocationMan
我有一个 iOS 应用程序,它每 4 小时左右触发一次本地通知。这似乎按预期成功运行。不过,我担心的是如果用户重新启动设备该怎么办。任何现有的本地通知是否仍然存在?以下是我如何触发本地通知:
我正在通过单击按钮安排基于位置的 UILocalNotification。但是当我尝试通过再次单击同一个按钮来取消 localNotification 时,它不会取消通知。我正在使用 UIApplic
我已使用 UILocalNotification() 创建了通知,并且我正在尝试清除收到的通知,而不删除计划的其余通知。 这是我尝试使用的代码: UIApplication.sharedApplica
我正在开发一个应用程序,该应用程序应该在 21 天内每天发送本地通知,但随后它应该停止发送一周。我知道我可以设置 fireDate 并且可以设置重复间隔,但是有什么方法可以设置像最后一个 fireDa
我有一个日期数组 NSArray *datesArray=[NSArray arrayWithObjects: @"2014-09-14 00:00:00",@"2014-08-21 07:12:36
我在我的应用程序中设置了一个警报,它会产生一个显示为横幅的本地通知。我怎样才能让它显示为必须单击以关闭它或理想情况下直接打开应用程序的警报? 这就是我目前设置通知的方式 window.plugin.n
我在我的 Ionic 应用程序中使用 ngCordova LocalNotification 插件,我试图让它以随机时间跨度重复。 例如,用户将触发通知在 1 分钟内启动,然后我需要在另外 2 分钟内
如果我从后台强行关闭我的应用程序。然后本地通知来了。如果点击本地通知,当应用程序在前台运行时我的方法不会被调用。我在 iOS 中较新。请帮忙。 -(void)application:(UIApplic
我正在开发一款应用,该应用会向用户发送本地通知,提醒他们某双鞋即将上市。 我认为应该在前一天晚上 8:00 发送通知。 这是我的日期字符串(称为 releaseDate): 2014-04-10 00
我正在使用经 Teleriks 验证的 LocalNotification 插件来将本地通知发送到运行我的应用程序的设备。我允许他们选择自己喜欢的通知间隔(1-2-3 或 4 小时),但我希望这些通知
我有一个场景,我安排了两个单独的 LocalNotifications。一个设置为在午夜十点开火,另一个每小时开火一次。我可以通过在安排通知时设置通知的 UserInfo 来区分它们。 对于每小时通知
我正在学习 PhoneGap,我尝试使用 Notification Local Plugin ,但对我不起作用。 我正在尝试构建我的应用程序,但它在 XML AndroidManifest.xml 中
我有这段代码每天早上 7 点运行通知,它获取当前日期,然后在到达设定时间时运行通知,我的问题是如果时间已经超过设定的运行时间,那么它每天都会在用户当前时间运行,而不是我早上 7 点的时间,这是我的代码
我在我的应用程序中使用 LocalNotification。它工作正常,但一旦设置了 ApplicationIconBadgeNumber,就无法将其从应用程序中删除。如何删除它? 最佳答案 您需要设
基于此教程Simple iOS Alarm Clock Tutorial,我实现了AlarmClock App。 该通知运行良好,但没有声音。 为了解决这个问题,我尝试过: Fix-1,并且未选中/选
我是一名优秀的程序员,十分优秀!