• 推送(PUSH)


    一、本地推送

    1.建立本地推送

     1     UILocalNotification *notification = [[UILocalNotification alloc] init];
     2     //推送的内容
     3     notification.alertBody = @"起床了";//设备收到本地通知时横额或锁屏时的主要文字内容
     4     notification.alertAction = @"起床了(ˇˍˇ)";//锁屏时显示的slide to后面的文字内容
     5     //推送时间
     6     notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
     7     //多久重复一次
     8     notification.repeatInterval = NSCalendarUnitHour;
     9     //启动图片
    10     notification.alertLaunchImage = @"AS_QQ@2x.png";
    11     //收到推送声音
    12     notification.soundName = UILocalNotificationDefaultSoundName;//默认的
    13     notification.soundName = @"shake_sound_male.wav";//自定义的
    14     //时区
    15     notification.timeZone = [NSTimeZone localTimeZone];
    16     //用户自定义数据
    17     notification.userInfo = @{kNotificationKey:@"notification1"};
    18     // 设置应用程序右上角的提醒个数
    19     notification.applicationIconBadgeNumber++;
    20     // 将通知添加到系统中
    21     [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    注意这个方法只有在程序启动之后才会执行,因此当程序处于后台时,该方法不会执行。

    有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)。

    因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。

    2.移除推送通知

    ① 暴力方法:直接取消所有推送

    1 [[UIApplication sharedApplication] cancelAllLocalNotifications];

     ② 移除个别推送通知:

     1     //获取所有的本地推送
     2     NSArray *allLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
     3     //取消指定推送
     4     //1.遍历所有的本地推送
     5     for (UILocalNotification *notification in allLocalNotifications)
     6     {
     7         //2.找指定的推送
     8         if ([notification.userInfo[kNotificationKey] isEqualToString:@"notification1"])
     9         {
    10             //3.取消推送
    11             [[UIApplication sharedApplication] cancelLocalNotification:notification];
    12         }
    13     }

     二、远程推送

    ① 概念:远程推送是在iOS 3.0以后被引入的功能,是当程序没有启动或不在前台运行时,告诉用户有新消息的一种途径,是从外部服务器发送到应用程序上的。一般说来,当要显示消息或下载数据的时候,通知是由远程服务器(程序的提供者)发送,然后通过苹果的推送通知服务APNS(Apple Push Notification Service)推送到设备的程序上。

    作为提供者为程序开发和部署推送通知,必须通过iOS Developer Program Portal获得SSL证书。每个证书限用于一个程序,使用程序的bundle ID作为标识。

    证书有两种用途的:一种是针对sandbox(用于开发和测试),另外一种针对发布产品。这两种运行环境拥有为各自指定的IP地址并且需要不同的证书。还必须为两种不同的环境获取各自的provisioning profiles。

    ② APNS:

    1.APNS提供了两项基本的服务:

    a.消息推送 --- 使用流式TCP套接字将推送通知作为二进制数据发送给APNs。消息推送有分别针对开发和测试用的sandbox、发布产品的两个接口,每个都有各自的地址和端口。不管用哪个接口,都需要通过TLS或SSL,使用SSL证书来建立一个安全的信道。提供者编制通知信息,然后通过这个信道将其发送给APNs。

    b.反馈服务 --- 可以得到针对某个程序的发送失败记录。提供者应该使用反馈服务周期性检查哪些设备一直收不到通知,不需要重复发送通知到这些设备,降低推送服务器的负担。

    2.APNS的工作机制:

    a.应用程序注册远程消息推送

     1     if ([UIDevice version] >= 8.0)
     2     {
     3         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
     4         //设置类型
     5         [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
     6         //注册远程推送
     7         [[UIApplication sharedApplication] registerForRemoteNotifications];
     8     }
     9     else
    10     {
    11         //iOS8以下必须设置类型
    12         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
    13          UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    14     }

    b.应用程序向APNS获取DeviceToken

    c.APNS接收后返回DeviceToken

     1 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
     2 {
     3     NSLog(@"%@",deviceToken);
     4     //提示:提交苹果服务器需要去除<>和空格 
     5     NSString *string = [NSString stringWithFormat:@"%@",deviceToken];
     6     string = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
     7     string = [string stringByReplacingOccurrencesOfString:@">" withString:@""];
     8     string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
     9     NSLog(@"%@",string);
    10 }

    d.应用程序将DeviceToken发送给PUSH服务端程序(Provider)

    e.Provider将DeviceToken和推送内容上传至APNS

    f.APNS将推送内容推送至所有注册过的iPhone上

  • 相关阅读:
    DRT移植各种成熟稳定的C工具包到DELPHI
    Delphi Event Bus 2.1发布了
    这个真恶心人!Delphi 11.1的Listview不能继承DynamicAppearance
    使用TBufferedLayout提升UI的显示效率
    Delphi 格式化代码调整每行的宽度
    Delphi 11.1 试用报告
    java POI Excel 导入
    Json在线生成Java实体类 http://www.esjson.com/jsontopojo.html
    正则校验记录先记录日后补充
    轻松搞懂POST与PUT的区别
  • 原文地址:https://www.cnblogs.com/caohexin-Blog/p/4984000.html
Copyright © 2020-2023  润新知