Local and Remote Notification

1 关于推送(简介)

推送的目的是为了让不在前台运行的app使用户能够知道我们有信息给他们。比如一条信息,即将到来的约会等。

推送给用户的类型有三种

Users can get notified in the following ways:

  • An onscreen alert or banner

  • A badge on the app’s icon

  • A sound that accompanies an alert, banner, or badge

推送分为两种,本地通知和远程推送,两者从用户角度来说是相同的,但从技术实现角度来讲,两者有很大的区别。

(1)本地通知:通过安装在设备上的app设定和负责推送。

(2)远程推送:通过自身的服务器,发送通知到Apple Push Notification Center(APNs),然后在通过APNs发送到用户的设备上。


2 本地通知和远程推送的实现(基本)

2.1本地通知

(1)在iOS8之前的版本中,本地通知是不需要注册的,但是在iOS8之后的版本,本地通知和远程通知统一使用UIApplication类中的

registerUserNotificationSettings:方法注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        //配置通知类型
        UIUserNotificationType types =  UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        //调用这个方法后,进入app时系统会弹出对话框来询问用户是否接收通知
        //当调用这个方法后,系统会回调AppDelegate中的application:didRegisterUserNotificationSettings:
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    return YES;
}

本地通知使用步骤:

(1)使用上面的方法注册通知的类型

(2)初始化UILocalNotification对象

(3)设置系统发送该通知的日期和时间,也就是fireDate属性。如果设置了timeZone属性为当地时区,那么当设备所在的时区改变时(或者重新设置了),系统会自动调整fireData。一般本地通知是基于时间来发送的,iOS8新出了一个region属性,下面再说。

(4)设置提醒,图标(右上角小红点),声音来提醒用户。

  1. alert

    再说,iOS8有新特性

  2. applicationIconBadgeNumber 属性用于显示图标右上角小红点中的数字

  3. soundName 发送通知时的声音,与上面两个结合使用 

(5)可选择的(到下面再说)

(6)iOS8之后,可以自定义通知的操作。

(7)使用scheduleLocalNotification:方法设定本地通知,该方法会根据fireDate中设置的时间来发送通知,也可以使用

presentLocalNotificationNow:立即发送通知。

使用cancelLocalNotification:或者cancelAllLocalNotifications:来用代码隐藏当前显示的通知。


2.2远程推送(使用远程通知需要申请推送证书,证书稍后再说,可以选择第三方极光推送或者百度云推送,降低开发成本)

设备app向APNs请求device token,获取后会返回给AppDelegate的代理方法,app需要接收device token,并将其发送给

推送服务器(provider);


使用步骤:(iOS8之后)

(1)使用2.1 (1)中的方法注册通知类型

(2)调用registerForRemoteNotifications向APNs注册接收推送

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        //配置通知类型
        UIUserNotificationType types =  UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        //调用这个方法后,进入app时系统会弹出对话框来询问用户是否接收通知
        //当调用这个方法后,系统会回调AppDelegate中的application:didRegisterUserNotificationSettings:
        [application registerUserNotificationSettings:settings];
        //注册远程通知
        [application registerForRemoteNotifications];
    } else {
        //在iOS8之前,使用registerForRemoteNotificationTypes:方法来代替前两个步骤
        //iOS8之前的方法
        UIRemoteNotificationType types = UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        [application registerForRemoteNotificationTypes:types];
    }
    return YES;
}

(3)成功注册后,APNsf返回device token,系统回调AppDelegate中的代理方法

(4)将device token提交给推送的服务器(与APNs相连的);

//向APNs注册,成功后返回deviceToken
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    //处理接收到到了deviceToken,需要上传给服务器(provider);
    ...
    NSLog(@"%ld",[deviceToken length]);
}

//向APNs注册,失败返回的结果
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"%@",error);
}

注意点:

(1)设备离线情况下,系统不会调用两个代理方法。

(2)如果设备通过wifi无法连接到APNs的5223端口,也会发生这种情况。(更换wifi或者使用蜂窝来解决)

服务器推送包括1.device token 2.payload


3通知的处理

用户收到通知后,会对其进行处理,处理通知分为以下几种情况。

1 发送通知的时候app没有在前台运行(此情况下,系统根据注册的通知类型来提醒用户)

(1)在iOS8之后的通知中,用户点击了自定义的Action button

系统调用application:handleActionWithIdentifier:forRemoteNotification:completionHandler:或者

application:handleActionWithIdentifier:forLocalNotification:completionHandler:。在两个方法中,可以

(2)用户点击了默认按钮或者点击了app图标

如果是Local Notification,系统会将Notification传递给application:didFinishLaunchingWithOptions:方法

如果是Remote Notification,调用会讲payload传递给application:didFinishLaunchingWithOptions:同时也会调用

application: didReceiveRemoteNotification:fetchCompletionHandler:方法


2通知发送的时候app运行在前台


对于以上所有的情况,实现

application:didReceiveRemoteNotification:fetchCompletionHandler:
  
application:didReceiveLocalNotification:

To handle notification actions.

application:handleActionWithIdentifier:forLocalNotification:completionHandler:

application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
 methods.

四个方法即可


4通知(iOS8之后新增的内容)

(1)自定义Notification Action,iOS8之前,默认只有一个动作。现在我们可以为通知增加自定义的内容。

为了在app中使用Notification actions,你必须定义这个actions,将他们按照categories分类,然后用UIApplication注册他们

为了定义一个notification Action,

一,你必须创建并实例化一个notification class实例,比如UIMutableUserNotfiicationAction。

二。你定义一个identifier,当它处理Action的时候会传给你的app,同时也定义一个本土化的Action button。

三。设置Action的激活模式,foreground(需要打扰用户)或者background

四。声明Action是否具有破坏性,他的按钮显示红色。是否需要用户输入密码




5证书申请


6APNs

5,6有空再写。





转载于:https://my.oschina.net/u/2474234/blog/511382

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值