码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发之-本地推送通知

时间:2017-06-07 14:19:40      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:注册   通过   oid   ret   播放   ini   user   程序启动   用户信息   

不多说直接上代码

 

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

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

    // Override point for customization after application launch.

    //如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)

    //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 - 私有方法

#pragma mark 添加本地通知

-(void)addLocalNotification{

    

    //定义本地通知对象

    UILocalNotification *notification=[[UILocalNotification alloc]init];

    //设置调用时间

    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知触发的时间,10s以后

    notification.repeatInterval=2;//通知重复次数

    //notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间

    

    //设置通知属性

    notification.alertBody=@"最近添加了诸多有趣的特性,是否立即体验?"; //通知主体

    notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数

    notification.alertAction=@"打开应用"; //待机界面的滑动动作提示

    notification.alertLaunchImage=@"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片

    //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音

    notification.soundName=@"msg.caf";//通知声音(需要真机才能听到声音)

    

    //设置用户信息

    notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息

    

    //调用通知

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

 

#pragma mark 移除本地通知,在不需要此通知时记得移除

-(void)removeNotification{

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

}

ios开发之-本地推送通知

标签:注册   通过   oid   ret   播放   ini   user   程序启动   用户信息   

原文地址:http://www.cnblogs.com/ios4app/p/6956485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!