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

iOS本地通知的实现

2015-11-12 14:58 531 查看
 /*

     @property(nonatomic,copy) NSDate *fireDate; //
设置本地推送的时间

     @property(nonatomic,copy) NSTimeZone *timeZone; //
时区

     

     @property(nonatomic) NSCalendarUnit repeatInterval;     //
重复多少个单元发出一次

     @property(nonatomic,copy) NSCalendar *repeatCalendar;   //
设置日期

     

     @property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);  //
比如某一个区域的时候发出通知

     @property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); //进入区域是否重复

     

     @property(nonatomic,copy) NSString *alertBody;     
消息的内容

     @property(nonatomic) BOOL hasAction;               
是否显示alertAction的文字(默认是YES)

     @property(nonatomic,copy) NSString *alertAction;   
设置锁屏状态下,显示的一个文字

     @property(nonatomic,copy) NSString *alertLaunchImage;  
启动图片

     

     @property(nonatomic,copy) NSString *soundName;      UILocalNotificationDefaultSoundName

     

     @property(nonatomic) NSInteger applicationIconBadgeNumber; 
应用图标右上角的提醒数字

     

     // user info

     @property(nonatomic,copy) NSDictionary *userInfo;

     */

    // 1.创建本地通知

    UILocalNotification *localNote = [[UILocalNotificationalloc]
init];

    

    // 1.1.设置什么时间弹出

    localNote.fireDate = [NSDatedateWithTimeIntervalSinceNow:5];

    

    // 1.2.设置弹出的内容

    localNote.alertBody =
@"吃饭了吗?";

    

   
// 1.3.设置锁屏状态下,显示的一个文字

    localNote.alertAction =
@"快点打开";

    

    // 1.4.显示启动图片

    localNote.alertLaunchImage =
@"123";

    

    // 1.5.是否显示alertAction的文字(默认是YES)

    localNote.hasAction =
YES;

    

    // 1.6.设置音效

    localNote.soundName =UILocalNotificationDefaultSoundName;

    

   
// 1.7.应用图标右上角的提醒数字

    localNote.applicationIconBadgeNumber =999;

    

    // 1.8.设置UserInfo来传递信息

    localNote.userInfo =
@{@"alertBody" : localNote.alertBody,@"applicationIconBadgeNumber" :
@(localNote.applicationIconBadgeNumber)};

    

    // 2.调度通知

    [[UIApplicationsharedApplication]
scheduleLocalNotification:localNote];

iOS8 以后需要设置

/**

 *  如果应用在后台,通过点击通知的时候打开应用会来到该代理方法

 *  如果应用在前台,接受到本地通知就会调用该方法

 *

 *  @param notification通过哪一个通知来这里

 */

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

//    if (application.applicationState == UIApplicationStateActive) return;

//    if (application.applicationState == UIApplicationStateInactive) {

//        NSLog(@"跳转");

//    }

    UILabel *label = [[UILabelalloc]
init];

    label.frame =
CGRectMake(0,
0, 300, 300);

    label.backgroundColor = [UIColorredColor];

    label.text = [NSStringstringWithFormat:@"%@", notification];

    label.font = [UIFontsystemFontOfSize:14.0];

    label.numberOfLines =
0;

    [self.window.rootViewController.viewaddSubview:label];

}

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

    /*

     UIUserNotificationTypeNone    = 0,     
没有,没有本地通知

     UIUserNotificationTypeBadge   = 1 << 0,
接受图标右上角提醒数字

     UIUserNotificationTypeSound   = 1 << 1,
接受通知时候,可以发出音效

     UIUserNotificationTypeAlert   = 1 << 2,
接受提醒(横幅/弹窗)

     */

    // iOS8需要添加请求用户的授权

    if ([UIDevicecurrentDevice].systemVersion.floatValue
>=8.0) {

        UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge
|UIUserNotificationTypeAlert | UIUserNotificationTypeSoundcategories:nil];

        [application registerUserNotificationSettings:settings];

    }

     // iOS 中本地通知启动的程序 launchOptions 的键名 UIApplicationLaunchOptionsLocalNotificationKey
会有值

    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {

        UILabel *label = [[UILabelalloc]
init];

        label.frame =
CGRectMake(0,
300, 300, 300);

        label.backgroundColor = [UIColorblueColor];

        label.text = [NSStringstringWithFormat:@"%@", launchOptions];

        label.font = [UIFontsystemFontOfSize:14.0];

        label.textColor = [UIColorwhiteColor];

        label.numberOfLines =
0;

        [self.window.rootViewController.viewaddSubview:label];

    }

    

    return
YES;

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