IOS网络笔记--本地推送

申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨

/*

  本地推送思路

 1 先注册推送通知 本地推送

 2 创建本地通知

 3 设置推送内容(alertBody)、推送数量(applicationIconBadgeNumber)、推送时间(fireDate)、推送周期(repeatInterval)、推送声音

 4 将推送发给系统,让系统去推送

 5 设置用户点击推送后实现的方法

 */



代码部分:

@implementation AppDelegate

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

    

    // 注册推送通知

    if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)])

    {

        // 本地推送

        UIUserNotificationSettings * setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

        

        [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    }

    

    // 创建本地通知

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

    

    // 推送内容

    myLocalNotifaction.alertBody = @"hhg 本地通知 today is 1024";

    

    // 推送数量

    myLocalNotifaction.applicationIconBadgeNumber = 1;

    

    // 推送时间

    myLocalNotifaction.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];

    

    // 推送周期

    myLocalNotifaction.repeatInterval = NSCalendarUnitMinute;

    

    // 推送声音

    myLocalNotifaction.soundName = UILocalNotificationDefaultSoundName;

    

    // 将推送发给系统,让系统去推送

    [[UIApplication sharedApplication]scheduleLocalNotification:myLocalNotifaction];

    

    /*  网络推送方法

     //网络推送

     UIRemoteNotificationType type = UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;

     [[UIApplication sharedApplication]registerForRemoteNotificationTypes:type];

     

     if (launchOptions != nil) {

     UILocalNotification *local = launchOptions[UIApplicationLaunchOptionsAnnotationKey];

     

     //UIApplicationLaunchOptionsAnnotationKey 本地推送的消息

     //UIApplicationLaunchOptionsRemoteNotificationKey 网络推送的消息

     

     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"程序启动时的本地推送" message:local.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

     [alertView show];

     

     }

     

     //取消网络推送

     //[[UIApplication sharedApplication]unregisterForRemoteNotifications];

     

     */

    return YES;

}


// 设置用户点击推送后实现的方法

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

{

    // 点击推送消息

    if ([[UIApplication sharedApplication]applicationState]==UIApplicationStateInactive) {

        UIAlertView *myAlertView =[ [UIAlertView alloc]initWithTitle:@"本地推送" message:@"感谢你玩这个app" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

        [myAlertView show];

        

        // 取消推送

        [[UIApplication sharedApplication]cancelLocalNotification:notification];

    }

    

}


附上完整代码:http://download.csdn.net/detail/csdn_hhg/9209599

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值