如何使用目标C实现LocalNotification?
我正试图在我的application
实施local notification
。 我不知道该怎么做,下面的代码,我正在使用新的数据到达过程,这里后如何实现通知过程,我需要在foreground
和background
两个时间的notification
下面我已经成功的为新的数据到达检查方法的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
值…一旦价值来了,我想show notification
。 我正在做的所有事情
谢谢
1)当应用程序closures时,安排一个本地通知,将在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];
现在让我们创build通知本身
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;
一旦我们创build了通知,我们需要安排与应用程序。
// 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]; }
我们为我们的应用程序添加了一个图标徽章,只有当应用程序在后台时才会显示此图标徽章。 一般来说,您想在用户打开应用程序并看到通知后closures图标。 我们还需要在应用程序委托中处理这个问题。
这两个方法会照顾它。
- (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 completionHandler:nil];
- 如何禁用iOS远程通知的默认通知警报视图?
- handleTurnEventForMatch:didBecomeActive:仅在某些时间到达的callback
- 我怎样才能访问默认的iOS声音设置为通知声音?
- 用NSNotification removeObserver …我做错了什么?
- UserNotification未显示(iOS 10)
- 当iOS应用程序在后台时,GCM推送通知
- didRegisterForRemoteNotificationsWithDeviceToken何时被调用?
- 无声的通知callback方法,只有当应用程序在后台调用
- Apple Watch:dynamic长时间不显示,当从通知中心推开