ios 本地推送

原创 2015年11月20日 17:46:54

    iOS上有两种消息通知,一种是本地消息(Local Notification),一种是远程消息(Push Notification,也叫Remote Notification),设计这两种通知的目的都是为了提醒用户,现在有些什么新鲜的事情发生了,吸引用户重新打开应用。本地推送也可以通过服务器控制,比如说如果有新消息了,推送消息,但是,前提是程序必须是打开的,而远程推送,是通过苹果APNS服务器,推送给手机,手机在推送给具体的哪个程序,一般远程推送用到的比较多.

   先介绍本地推送:

//接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"%@",notification.alertBody);
    UILabel*label = [[UILabel alloc]init];
    label.frame = CGRectMake(0,0, 160,20);
    label.layer.cornerRadius =10;
    label.backgroundColor = [UIColor blackColor];
    label.text = notification.alertBody;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
     
    [self.window addSubview:label];
}
1.首先,先在appdelegate中注册:
<span style="color:#006600;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];//注册本地推送
    return YES;
}</span>
2.然后,在具体的viewcontroller中实现推送:
<span style="color:#006600;">- (IBAction)localPushNow:(id)sender {
  
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //本地推送
        UILocalNotification*notification = [[UILocalNotification alloc]init];
        NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
        if (notification != nil) {
            notification.fireDate = pushDate;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.repeatInterval = kCFCalendarUnitDay;
            notification.soundName = UILocalNotificationDefaultSoundName;
            notification.alertBody = @"hello,world";
            notification.applicationIconBadgeNumber = 0;
            NSDictionary*info = [NSDictionary dictionaryWithObject:@"test" forKey:@"name"];
            notification.userInfo = info;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
             
        }
    });
 
}</span>
3.在appdelegate中会接收到推送信息:

//接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"%@",notification.alertBody);
    UILabel*label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 160, 20);
    label.layer.cornerRadius = 10;
    label.backgroundColor = [UIColor blackColor];
    label.text = notification.alertBody;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
     
    [self.window addSubview:label];
}
过程中可能会出现如下状况:

Attempting to schedule a local notification……with a sound but haven't received permission from the user to play sounds

Attempting to schedule a local notification……with an alert but haven't received permission from the user to display alerts

可能是因为你没有注册,或者设置中没有开启推送功能,


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

iOS 推送测试php

iOS 如何让APP 删除后不接受 APNS 推送消息

首先简单说下iOS APNS:      Apple 使用公共密钥数字证书对来自 iOS应用程序的推送请求进行身份验证,所以您首先需要创建身份验证密钥,并向 Apple注册它们。我将在下一节中花...

极光推送Android IOS SDK V2.1.9

  • 2016-10-06 16:34
  • 7.94MB
  • 下载

iOS 环信apns离线推送相关deviceToken和证书等问题

用过环信的知道,环信在APP挂在后台超过3分钟或者被kill之后,是不会收到消息了的,如果想要能继续接受消息,需要注册apns离线推送。在调试期间遇到一些棘手问题,现总结如下: 先附上环信社区配置ap...

ios推送jar包

  • 2014-07-28 13:59
  • 2.17MB
  • 下载

【WWDC2016 Session】iOS 10 推送Notification新特性

投稿文章,作者:一缕殇流化隐半边冰霜(微博) 前言 在今年6月14号苹果WWDC开发者大会上,苹果带来了新的iOS系统——iOS 10。苹果为iOS 10带来了十大项更新。苹果高级副总裁C...

ios推送客户端

  • 2014-04-15 17:47
  • 69KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)