您的位置:首页 > 其它

本地通知

2015-08-08 16:54 190 查看
在很多App中都会需要用到推送服务,推送通知的作用是不在前台运行的app,知道app内部发生了什么事情。但真真开发中都是远程推送。不过今天先要说的是本地推送,推送通知的显示有‘横幅’和’Alert’的形式,具体设置是这样的:



同时推送的通知也有几种呈现效果:



用户也是可以设置是否开启这些功能:



好的,前面已经说的一些基本的概念东西,下面就来上代码具体看看本地通知是个什么情况:

1.首先是新建一个Project,命名为LocalNotificationPush



2.打开Main.storyboard,设置为iPhone6的尺寸,拖入一个UIButton控件:



3.创建一个通知并配置一些基本的信息

- (IBAction)creatLocalNoti
{

//创建一个本地通知
UILocalNotification *localNoti=[[UILocalNotification alloc] init];

//通知的内容
localNoti.alertBody=@"好好学习";

//通知的额外信息
localNoti.userInfo=@{@"info":@"继续敲代码"};

//通知的开始时间
localNoti.fireDate=[NSDate dateWithTimeIntervalSinceNow:5.0];

//使用Application的单例执行一个本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
}


运行程序,然后点击“创建本地通知”的按钮,系统会在控制台会输出一些详情信息,我们来看下:

2015-08-08 16:09:55.107 LocalNotificationPush[3955:185805] Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7ff6d2639bd0>{fire date = 2015年8月8日 星期六 格林尼治标准时间下午4:10:00, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = 2015年8月8日 星期六 格林尼治标准时间下午4:10:00, user info = {
info = "\U7ee7\U7eed\U6572\U4ee3\U7801";
}} with an alert but haven't received permission from the user to display alerts


可以看出是程序没有获得权限或用户的许可,控制台才会输出这些信息。在iOS8中,系统对通知推送做了一些改变,我们在使用推送服务之前是需要先取得用户的许可的,我们可以在AppDelegate.m中通过调用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

//设置本地通知的参数
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];

//注册通知
[application registerUserNotificationSettings:settings];

return YES;
}


然后我们再运行程序,可以显示下面的界面:



我们选择“好”,这项设置我们只需设置一次即可,下次再运行这个程序就不会出现这个界面。

进入到主界面之后我们就点击“创建本地通知”的按钮,然后进入后台(正在运行的App是在前台的,运行在前台的程序是无法接收到推送通知的)。进入后台的快捷键是Command+Shift+H。我们设置的时间是点击”创建本地通知“按钮5秒之后显示通知,5s之后在手机屏幕的上方我们会看到:



然后我们再点击屏幕上方显示的通知,系统会自动打开通知对应的程序主界面,我们是无法看到我们在Controller(控制器)里面设置的额外信息(“继续敲代码”)的,我们在这里又得需要在AppDelegate里面闪现系统的一个方法:

//接收到本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *userInfo=notification.userInfo[@"info"];
[self showAlertViewWithTitle:@"定时提醒" message:userInfo];

}
//显示提醒的内容
-(void)showAlertViewWithTitle:(NSString *)title   message:(NSString *)message
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"知道" otherButtonTitles:nil, nil];
[alertView show];
}


接着你会看到我们之前设置的通知包含的额外信息内容:



上述这些都是在程序还未被关闭,程序运行在后台的情况下,可以看到设置的推送通知的内容。

那如果点击“创建本地通知”按钮之后,我们然程序不是进入后台,而是直接关闭程序,情况又会怎么样呢?

1.点击“创建本地通知”按钮;

2.使用快捷键Command+Shift+H(H需要快速按两次),进入到所有运行在后台的程序界面,然后快速的将我们打开的程序向上滑动,这样便可关闭程序。



稍等一会我们同样可以在屏幕上方看到推送通知的信息,但我们再次点击这个通知的时候,系统会重新打开程序,但却没有我们设置的额外信息的提醒框,要想在程序已经关闭,同时又能正常收到所有的通知信息和内容的时候我们还得在AppDelegate里面做些工作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

//设置本地通知的参数
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];

//注册通知
[application registerUserNotificationSettings:settings];

if (launchOptions)
{

UILocalNotification *localNoti= launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
NSString *info=localNoti.userInfo[@"info"];
[self showAlertViewWithTitle:@"定时提醒" message:info];
}

return YES;
}


因为程序一运行会来到这个方法,我们再次进入程序的时候我们就在这里面做功夫。

总结一下,就是:



好的,这样,无论是程序进入后台还是y已经关闭我们都可以收到推送通知的信息了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: