您的位置:首页 > 其它

本地推送消息

2015-12-02 20:59 246 查看
        iOS上有两种消息通知,一种是本地消息(Local Notification),一种是远程消息(PushNotification,也叫Remote Notification),设计这两种通知的目的都是为了提醒用户,现在有些什么新鲜的事情发生了,吸引用户重新打开应用。

  本地消息什么时候有用呢?譬如你正在做一个To-do的工具类应用,对于用户加入的每一个事项,都会有一个完成的时间点,用户可以要求这个To-do应用在事项过期之前的某一个时间点提醒一下它。为了达到这一目的,App就可以调度一个本地通知,在时间点到了之后发出一个Alert消息或者其他提示。
        主要介绍本地通知的注册以及使用,由于初学,不合理的地方还望指正。本地通知的使用主要分这么几个步骤:

步骤一:注册

在AppDelegate.m里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

//注册本地推送
if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication]registerUserNotificationSettings:settings];
}

return YES;
}

步骤二:添加
在添加通知的方法里

- (IBAction)addNotification:(UIButton *)sender
{
if(!_flag)
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//设置启动时间
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]];
//设置时间
localNotification.timeZone = [NSTimeZone localTimeZone];
//重复间隔
localNotification.repeatInterval = kCFCalendarUnitMinute;
//提醒的文字
[localNotification setAlertBody:[NSString stringWithFormat:@"%@",self.inforText.text]];
//播放声音
[localNotification setSoundName:UILocalNotificationDefaultSoundName];

localNotification.applicationIconBadgeNumber = 1;
//参数信息,用于标识该通知
NSDictionary *dictUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"Alert",@"type", nil];
localNotification.userInfo = dictUserInfo;
//添加通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"flag"];
_flag = YES;
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"通知添加成功,5秒后看效果" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"只能添加一个通知!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}
}


步骤三:处理
在AppDelegate.m里面

<span style="font-size:18px;"><span style="font-size:18px;">//当系统处于运行状态,收到通知可以弹出一个警告框</span></span>
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
[[[UIAlertView alloc]initWithTitle:@"通知" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]show];
}
//当系统从后台或非运行状态进入前台,会标置为0
- (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.
application.applicationIconBadgeNumber = 0;
}

步骤四:取消

在取消通知的方法里

- (IBAction)cancelNotification:(UIButton *)sender
{
if (_flag) {
NSArray* arrayOfLocation = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification* notify in arrayOfLocation) {
if ([[notify.userInfo objectForKey:@"type"] isEqualToString:@"Alert"]) {
[[UIApplication sharedApplication] cancelLocalNotification:notify];
}
}
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"flag"];
_flag = NO;
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"通知取消成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}else{
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"没有通知可以取消" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alertView show];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: