博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PUSH
阅读量:6857 次
发布时间:2019-06-26

本文共 3262 字,大约阅读时间需要 10 分钟。

在程序退到后台的时候马上弹出一个PUSH

 

- (void)applicationDidEnterBackground:(UIApplication *)application {
/* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */ NSLog(@"Application entered background state."); // bgTask is instance variable NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil); bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask]; self->bgTask = UIBackgroundTaskInvalid; }); }]; dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif) {
localNotif.alertBody = @"alertBd"; localNotif.alertAction = @"alertAtn"; localNotif.soundName = @"test.aif"; localNotif.applicationIconBadgeNumber = 1; [application presentLocalNotificationNow:localNotif]; [localNotif release]; break; } } [application endBackgroundTask:self->bgTask]; self->bgTask = UIBackgroundTaskInvalid; }); }

因为事件发生,弹出很多PUSH

在应用程序代理头文件中

UILocalNotification *localNotification;  NSTimer *timer;

在实现文件中:

- (void)applicationDidEnterBackground:(UIApplication *)application {
/* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */ [self saveContext]; if (!localNotification) {
localNotification = [[UILocalNotification alloc] init]; } old_count = 0; if (!timer) {
timer = [[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(handle:) userInfo:nil repeats:YES] retain]; } UIApplication* app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask; // Request permission to run in the background. Provide an // expiration handler in case the task runs long. // NSAssert(bgTask == UIBackgroundTaskInvalid, nil); bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
// Synchronize the cleanup call on the main thread in case // the task actually finishes at around the same time. dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
// [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
// [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }); }

转载于:https://www.cnblogs.com/pengyingh/articles/2375664.html

你可能感兴趣的文章
Linux环境下安装mysql并分配用户权限
查看>>
Linux中动态探针kprobes
查看>>
div盒子水平垂直居中方法
查看>>
VR视频创作,想说爱你不容易
查看>>
笨办法学 Python · 续 练习 23:三叉搜索树
查看>>
可穿戴设备不能急于戴上“智能”的高帽子
查看>>
扒一扒政府的专用黑客组织ZooPark
查看>>
Ubuntu下Deb软件包相关安装与卸载
查看>>
互联网转型需要微服务架构
查看>>
Airbus Safran Launchers:选择西门子彻底改革产品生命周期流程
查看>>
文本数据的分分合合
查看>>
无损音乐知识收集2(转)
查看>>
Java几种常见的四舍五入的方法
查看>>
vim 工具(一)
查看>>
MySQL常用命令
查看>>
zabbix snmp监控
查看>>
MySQL单实例重置密码的两种方法
查看>>
一段典型的PHP程序都包含那些内容呢?
查看>>
python paramiko模块讲解
查看>>
Windows Phone 7 数据绑定的简单介绍
查看>>