您的位置:首页 > 移动开发 > IOS开发

[IOS]本地通知

2015-09-24 16:46 507 查看
[IOS]本地通知
Demo:http://download.csdn.net/detail/u012881779/9136957

本地通知一般用于闹钟、待办事项、延迟响应等定时提醒或操作,能有效的减轻服务端压力。

和很多第三方的推送工具一样,只在AppDelegate.m中就能完成操作。

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@property (nonatomic,strong) ViewController *viewc;
@property (nonatomic,strong) UINavigationController *nav;
@property (nonatomic,strong) UIAlertView *alert;
@end

@implementation AppDelegate
@synthesize viewc = _viewc;
@synthesize nav = _nav;
@synthesize alert = _alert;

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor =[UIColor whiteColor];

_viewc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
_nav =[[UINavigationController alloc] initWithRootViewController:_viewc];
[_nav setNavigationBarHidden:YES];
self.window.rootViewController = _nav;
[self.window makeKeyAndVisible];

/*
如果已经获得发送通知的授权则创建本地通知,否则请求授权
(注意:如果不请求授权在设置中是没有对应的通知设置项的,
也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
*/
if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}else{
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

return YES;
}

#pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行)
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
if (notificationSettings.types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}
}

#pragma mark 进入前台设置消息信息
-(void)applicationWillEnterForeground:(UIApplication *)application{
//进入前台取消应用消息图标
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
}
#pragma mark 进入后台设置消息信息
- (void)applicationWillResignActive:(UIApplication*)application{
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
}

#pragma mark 添加本地通知
-(void)addLocalNotification{

//定义本地通知对象
UILocalNotification *notification=[[UILocalNotification alloc] init];
/*
设置调用时间
*/
//通知触发的时间,10s以后
notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];
//通知重复间隔 0 NSCalendarUnitSecond
notification.repeatInterval=NSCalendarUnitSecond;
//当前日历,使用前最好设置时区等信息以便能够自动同步时间
notification.repeatCalendar=[NSCalendar currentCalendar];

/*
设置通知属性
*/
//通知主体
notification.alertBody=@"最近挺忧郁的,需要治疗吗?";
//应用程序图标右上角显示的消息数
notification.applicationIconBadgeNumber=1;
//待机界面的滑动动作提示
notification.alertAction=@"打开APP";
//通过点击通知打开应用时的启动图片,这里使用程序启动图片
notification.alertLaunchImage= @"Default";
//收到通知时播放的声音,默认消息声音
notification.soundName=UILocalNotificationDefaultSoundName;
//通知声音(需要真机才能听到声音)
notification.soundName=@"msg.caf";

/*
设置用户信息
*/
NSDictionary *info = [[NSDictionary alloc] initWithObjectsAndKeys:@"101",@"id",@"Gamin",@"user",@"message",@"msg", nil];
//绑定到通知上的其他附加信息
notification.userInfo=info;

/*
调用通知
*/
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

#pragma mark:接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
NSLog(@"\n%@",notification.userInfo);

if([[notification.userInfo objectForKey:@"id"] intValue] == 101){
if(!_alert){
_alert = [[UIAlertView alloc] init];
}
_alert.title = @"接收到本地推送";
_alert.message = notification.alertBody;
[_alert addButtonWithTitle:@"确定"];
[_alert addButtonWithTitle:@"取消"];
[_alert show];
}
// 图标上的数字减1
application.applicationIconBadgeNumber -= 1;

//移除所有本地通知
[self removeNotification];
}

#pragma mark 移除所有本地通知
-(void)removeNotification{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}

#pragma mark 移除某条本地通知
-(void)removeOneNotification{
//解除本地推送
UIApplication *app = [UIApplication sharedApplication];
//获取本地推送数组
NSArray *localArray = [app scheduledLocalNotifications];
//声明本地通知对象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"id"];
if ([inKey intValue] == 101) {
if (localNotification){
localNotification = nil;
}
localNotification = noti;
break;
}
}
}

//判断是否找到已经存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
}
}
}
 

示意图:
通知页面:



锁屏页面:



进入APP:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: