ios 本地通知 UILocalNotification
目前能实现,在应该关闭的情况下弹出提示框,声音,和上面的横幅。。。
在打开的情况下弹出提示框,,。
?
但是现在还没有实现,自定义在后台的时候显示的是提示框还是上面的横幅,,,只能用户自己在手机中设置是哪一种形式。。。。等以后我找到了再改博客。。。
?
?
?用的时候很好用:一个方法就行;
?NSDictionary *myDic = [NSDictionarydictionaryWithObject:@"apple"forKey:@"a"];
? ? NSString *myString = @"my body";
?? ?
?
? ??[MyLocalNotificationcreatLocalNotification:2.0timeZone:[NSTimeZonesystemTimeZone] userInfor:myDic alertBody:myString];
?
?
?
?
?
需要一个方法,,我把它写到一个类里面了。
@interface MyLocalNotification : NSObject
?
+(void)creatLocalNotification:(NSTimeInterval)timeInterval timeZone:(NSTimeZone*)zone userInfor:(NSDictionary*)userDic alertBody:(NSString*)body;
?
?
@end
#import "MyLocalNotification.h"
?
@implementation MyLocalNotification
?
?
+(void)creatLocalNotification:(NSTimeInterval)timeInterval timeZone:(NSTimeZone*)zone userInfor:(NSDictionary*)userDic alertBody:(NSString*)body
{
? ? UILocalNotification *notification=[[UILocalNotificationalloc] init];//新建通知
?? ?
?? ?
? ? notification.soundName= UILocalNotificationDefaultSoundName;//声音,可以换成alarm.soundName = @"myMusic.caf"?
?? ?
? ? notification.fireDate=[[NSDate date] dateByAddingTimeInterval:timeInterval];//距现在多久后触发代理方法
? ? notification.timeZone=zone;//设置时区
? ? notification.userInfo=userDic;//在字典用存需要的信息
?? ?
? ? //去掉下面2行就不会弹出提示框
? ? notification.alertBody=body;//提示信息弹出提示框
? ? notification.alertAction = @"打开";? //提示框按钮
//? ? notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失
?? ?
? ? [[UIApplicationsharedApplication] scheduleLocalNotification:notification];//将新建的消息加到应用消息队列中
?? ?
?? ?
?? ?
// ? ? NSArray*arrSchedule=[[UIApplication sharedApplication]scheduledLocalNotifications];//获得所有已注册但未到时提醒的本地消息
?? ?
//? ? 3.消息接受
//? ? - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
//? ? 这个方法是消息到时间后进此代理,在这里可以设置他弹框或者别的(你想做的操作)
//? ? **注意如果在此程序在后台运行时将不会听你的话。可以在系统设置中进行消息通知设置是让他弹框还是横条提醒随你设置。此时已提醒的消息会显示在手机的消息通知栏中(只要你不手动将此消息取消则他会一直显示在消息通知栏中)
//? ? 其中有一点很重要UIApplicationState state = application.applicationState
//? ? 这个状态分为UIApplicationStateActive(活动在前台)UIApplicationStateBackground(程序进入后台)UIApplicationStateInactive(进入消息通知栏时点击某条消息)
//? ? 4.取消消息
//? ? - (void)cancelLocalNotification:(UILocalNotification *)notification;//取消一条消息
//? ? - (void)cancelAllLocalNotifications;//取消所有已发出的消息(此时消息栏中不会有消息提醒)
}
?
?
?
?
@end
?
?
下面的这几个方法,必须在应用打开进去的情况下才能执行。
?
//? ? 3.消息接受
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
?? ?
? ? //如果想让手机运行着这个项目的情况下,必须在下面写弹出框。并且是在打开。如果在后台,也不用。
? ? [ViewControllerFactory showMessageAlert:notification.alertBody];
?? ?
?? ?
}
//? ? 这个方法是消息到时间后进此代理,在这里可以设置他弹框或者别的(你想做的操作)
//? ? **注意如果在此程序在后台运行时将不会听你的话。可以在系统设置中进行消息通知设置是让他弹框还是横条提醒随你设置。此时已提醒的消息会显示在手机的消息通知栏中(只要你不手动将此消息取消则他会一直显示在消息通知栏中)
//? ? 其中有一点很重要UIApplicationState state = application.applicationState
//? ? 这个状态分为UIApplicationStateActive(活动在前台)UIApplicationStateBackground(程序进入后台)UIApplicationStateInactive(进入消息通知栏时点击某条消息)
?
?
//? ? 4.取消消息
- (void)cancelLocalNotification:(UILocalNotification *)notification{
?? ?
}//取消一条消息
- (void)cancelAllLocalNotifications{
?? ?
?
}//取消所有已发出的消息(此时消息栏中不会有消息提醒)
?
?
?
?//下面是我刚看到的,别人写的挺好的。
?UILocalNotification *notification=[[UILocalNotification alloc] init];
? ?? ???if (notification!=nil)?
? ?? ???{
? ?? ?? ?? ?
? ?? ?? ?? ?NSDate *now=[NSDate new];
? ?? ?? ???
? ?? ?? ?? ?//notification.fireDate=[now addTimeInterval:period];
? ?? ?? ?? ?notification.fireDate = [now dateByAddingTimeInterval:period];
? ?? ?? ?? ?NSLog(@"%d",period);
? ?? ?? ?? ?notification.timeZone=[NSTimeZone defaultTimeZone];
? ?? ?? ?? ?notification.soundName = @"ping.caf";
? ?? ?? ?? ?//notification.alertBody=@"TIME!";
? ?? ?? ?? ?
? ?? ?? ?? ?notification.alertBody = [NSString stringWithFormat:@"@%时间到了!",nameStr];
? ?? ?? ?? ?
? ?? ?? ?? ?NSDictionary* info = [NSDictionary dictionaryWithObject:uniqueCodeStr forKey:CODE];
? ?? ?? ?? ?notification.userInfo = info;
? ?? ?? ?? ?
? ?? ?? ?? ?[[UIApplication sharedApplication] scheduleLocalNotification:notification];? ?? ?
? ?? ?? ?? ?
? ?? ???}?
设置的时间到了以后,会自动在桌面弹出一个提示框,点显示后,就可以启动软件。然后在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
? ? UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
? ? if (localNotif)?
? ? {
? ?? ???NSLog(@"Recieved Notification %@",localNotif);
? ?? ???NSDictionary* infoDic = localNotif.userInfo;
? ?? ???NSLog(@"userInfo description=%@",[infoDic description]);
? ?? ???NSString* codeStr = [infoDic objectForKey:CODE];
? ? }
}里,对lanchOptions进行处理,找到它里面的信息,就可以拿到设置时的需要处理的东西,就可以继续操作了。
如果此时你的客户端 软件仍在打开,则会调用
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
}
一样的处理方法。
?