推送通知

推送通知:

什么时候使用本地通知:

一般是一些数据量小的,不是及时消息的就可以,结合本地的数据库 的数据 和时间准时发送。 

什么时候使用远程通知:

如果是动态的,及时的,数量大的,需要使用远程通知。




1. 以前OC我们经常接触的 通知模式(机制): 发送通知的一方,去查: 通知名称,谁监听的。

 2. 这次要学习的推送通知, 可以理解为:

有界面显示,向用户推送一条消息来通知用户某件事情, 分为在前台运行, 在后台运行,就是说APP到后台或者关闭,人就可以接收到一个通知。 

分为本地推送(不连网)和远程推送(需要网络,APNs, apple push notification services苹果推送通知服务);换个角度,就是如果脱离网络,远程推送就不能实现了, 可以从这个角度多使用本地通知。

应用场景:可以按照你的计划通知你做什么,任务管理器或者APP里面你的计划,健身,电影票,商品的新品通知,QQ或者微信的消息通知。

3. 通知的样式:








可以在手机的通知中心里面,改变通知的显示效果。 



解释一下这个界面都是什么意思:



推送通知的使用细节:





发送通知:

 // 1. 创建本地通知

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

    // 1.1 设置通知内容

    localNotice.alertBody=@"顺子要不要?";

    // 1.2 设置通知发送时间

    localNotice.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];

    /** ---------------------------------额外属性补充--------------------------------------------- */

    // 滑动后面的文字

    localNotice.hasAction=yes; //如果是false下面就没有反应。默认是yes,所以这句话不用写。

    localNotice.alertAction=@"回复";//默认是滑动来查看。

 // 通知标题(8.2)

    if(isiOS(8.2))

    {

        localNotice.alertTitle=@"斗地主";

    }

改变了通知中心通知的标题。

gai

    // 接收到通知时,播放的音频

    localNotice.soundName=@"buyao.wav";//通知的声音,格式aac等等。

    // 设置提醒数字,图标右上角的数字,改成0就是不显示,就是清除的意思。

    localNotice.applicationIconBadgeNumber = 2;

    // 用来传递参数

    localNotice.userInfo= @{

                             @"message":@"顺子要不要?"

                             };

    // 指定通知使用哪个操作组//通过这里和appdelegate的组做关联

    localNotice.category=@"select";

    // 2. 发送本地通知

    // 通知是一个应用程序级别

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotice];

取消发送通知:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

查看通知:

 NSLog(@"所有的通知: %@",[[UIApplication sharedApplication]  scheduledLocalNotifications ]);




appdelegate里面的这句话,可以改变右标的数字为0

如何监听通知:

1.第一种情况,前台,只有提醒数字,后台才会点击就打开,完全关闭是第二种情况

2.第二种情况:完全关闭的情况,想点击看到通知内容,打开聊天窗口。

如果你想做出来这种效果,在通知的时候直接点击回复,或者回消息。


// 自定义方法-获取用户推送通知授权

-(void)registerAutor{

    //创建一组操作行为

    UIMutableUserNotificationCategory * category = [[UIMutableUserNotificationCategory alloc] init];

    //设置组标识

    category.identifier=@"select";

    //设置组里面的操作行为

    UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];

    action1.title=@"不要";

    action1.identifier=@"action1";

    action1.activationMode = UIUserNotificationActivationModeForeground;

    action1.authenticationRequired = YES;

    action1.destructive=YES;

    UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];

    action2.title=@"王炸";

    action2.identifier=@"king";

    action2.activationMode = UIUserNotificationActivationModeBackground;

    action2.authenticationRequired = false;

    action2.destructive=NO;

    action2.behavior = UIUserNotificationActionBehaviorTextInput;

    action2.parameters = @{@"UIUserNotificationTextInputActionButtonTitleKey":@"fasong"};

    NSArray * actions = @[action1 ,action2];

    [categorysetActions:actions forContext:UIUserNotificationActionContextDefault];

    //设置附加操作行为

    NSSet<UIUserNotificationCategory *> * categories = [NSSet setWithObjects:category, nil];

    //设置 请求权限 的对象

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

    //注册设置

        [[UIApplication sharedApplication] registerUserNotificationSettings:sets];

}

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

    [self registerAutor];

}

怎么监听你到底点击了什么按钮呢?实现一下代理方法,2个都要写:

-(void)application:(UIApplication*)application handleActionWithIdentifier:(NSString*)identifier forLocalNotification:(UILocalNotification*)notification completionHandler:(void(^)())completionHandler{

    NSLog(@"id ===%@",identifier);

    NSLog(@"notification ===%@",notification);

    // 调用系统的完成代码块,告诉系统,已经处理完毕

    completionHandler();

}

//9.0出来的。

-(void)application:(UIApplication*)application handleActionWithIdentifier:(NSString*)identifier forLocalNotification:(UILocalNotification*)notification withResponseInfo:(NSDictionary*)responseInfo completionHandler:(void(^)())completionHandler{

        NSLog(@"id ===%@",identifier);

        NSLog(@"responseInfo ===%@",responseInfo);

        // 调用系统的完成代码块,告诉系统,已经处理完毕

        completionHandler();

}


怎么做输入框呢?

 action2.behavior = UIUserNotificationActionBehaviorTextInput;

发送了什么呢?上面代码的这个参数,就是发送了什么了。

  NSLog(@"responseInfo ===%@",responseInfo);

怎么改文字呢?这个就是输入文字右边的按钮的文字,可以改的方法了。

 action2.parameters = @{@"UIUserNotificationTextInputActionButtonTitleKey":@"fasong"};


vc的通知 怎么和appdelegate做关联呢。

 localNotice.category = @"select";//通过这里和appdelegate的组做关联

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~本地通知over~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~远程推送通知begin~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

就是从远程服务器推送给客户端的通知,苹果推送服务(APNs),远程推送服务。

因为传统的通知的局限性: 只要关闭了app,就无法跟app的服务器沟通了,无法从服务器上获得最新的数据。

但是我们的远程推送: 只要联网了,都一直建立连接,一直可以有通知接收和发送。

因为,我们的苹果设备,在联网状态下,都会与苹果的服务器建立长连接。


长连接平时的作用:

时间校准,系统升级,查找我的iphone.

服务器一更新,客户端 保持最新状态,传输速度快。


怎么运转的:

一、iOS端推送原理(以QQ为例) :

1.  App的用户 把UDID和Bundle Id 给APNs苹果服务器,APNs服务器把这个加密之后生成一个deviceToken 发送给我们自己的服务器;  

2.我们自己的服务器, 存到数据库里面, 例如, id 1, name 张三, qq 12345, deviceToken 888;

3.当我们需要推送消息时:

    当用户在线,APNs直接发送: 服务器将推送消息按照指定格式 和 devicetoken打包 发给APNs,  A和APNs是长连接,是可以直接发送的; 

    当用户不在线,服务器发送:  服务器去自己的数据库 查找用户的QQ号对应的deviceToken,然后将deviceToken发送给APNs,APNs会解析出UDID和Bundle Id,那么就可以找到对应的设备, 对应的QQ号,直接发送消息了。

二、以服务器的角度,记录需要的东西:

1. 证书(苹果开发人员提供);

2. deviceToken(苹果开发人员提供)———通过这个找到 设备和app ;

3. 服务器发送通知的指定格式

{"aps":{"alert":"hello.hello.hello..","badge":1,"sound":"default","content-available":"every word"}}




第一件事, 申请证书

(证书;        appleid(选择通知;打开appid,创建推送的证书文件), 或者直接推送证书,可以直接以创建证书的方式创建推送证书,所有的推送证书都要绑定appid,会有让你选择的过程,选择csr文件,download;                   描述文件,证书全选,设备全选,下载证书。 )

           开发证书。appid.    推送证书。 描述文件 。

           证书的三种: ios app development 开发证书, appstore发布证书, ad hoc打包测试证书。


第二件事, 向苹果的服务器,请求deviceTolen,发送给公司的服务器

首先要保证你的项目的bundle id名字, 和你的推送证书的bundle id名字是一致的。 

#define isIOS(version) ([[UIDevice currentDevice].systemVersion floatValue] >= version)  这句话是判断ios的情况。

第三件事,   监听用户对通知的点击。 

(服务器发送通知的指定格式)

只有连接pushmebaby,在devicetoken  里面加入刚才获取的devicetoken,

                                      在payload里面写入格式:{"aps":{"alert":"hello.hello.hello..","badge":1,"sound":"default","content-available":"every word"}}

发现,然后,在后台收到通知,点击通知,进入前台, 会进入监听的通知的方法。 


监听的时候,如果在后台,必须点击,怎么可以不点击,就执行监听的方法呢。 必须满足三个条件:

// 1> 必须勾选后台模式Remote Notification ;

// 2> 告诉系统是否有新的内容更新(执行完成代码块)

// 3> 设置发送通知的格式("content-available":"随便传")

self.payload = @"{\"aps\":{\"alert\":\"This is some fancy message.\",\"badge\":1,\"content-available\":\"xx\"}}";

{"aps":{"alert":"8888888","badge":8,"content-available":"9999"}}

{"aps":{"alert":"4444","badge":"777"}}

0ab90ec3 b1261fd2 ae5acea9 53f1f40b 39d556d8 02df246b d4ae7c8e c7e1342b


满足这三点,就可以不用点击,就直接监听了。 



必须写的这个方法,就是这个作用:

//执行completionHandler有两个目的

//1> 系统会估量App消耗的电量,并根据传递的UIBackgroundFetchResult 参数记录新数据是否可用

//2> 调用完成的处理代码时,应用的界面缩略图会自动更新

//注意:接收到远程通知到执行完网络请求之间的时间不能超过30秒







极光推送:







©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。