首页 技术 正文
技术 2022年11月13日
0 收藏 400 点赞 4,245 浏览 3292 个字

一.APNS 远程推送

1.所有的苹果设备,在联网状态下,都会与苹果服务器建立长连接.

2.长连接:就是只要联网了,就一直建立连接.

3.长连接的作用:时间校准,系统升级,查找我的iPhone.

4.长连接的好处:数据传输速度快,数据保持最新状态.

5.  DeviceToken 处理流程如下:

1.手机注册远程通知到APNS服务器.
2.APNS服务器向手机返回DeviceToken.
3.手机再将DeviceToken发送到你的App.
4.你的App再发送DeviceToken到数据提供商.

6. 获取DeviceToken的流程需要文件如下:

1.电脑真机调试证书(开发证书或发布证书).
2.设备的UUID.
3.APPID需要Bundle identifier 也就是Xcode中的,必须是全名,不能有*号代替,如com.ling.identifier
4.电脑描述文件(CertificateSigningRequest.certSigningRequest文件).
5.开发推送证书和发布推送证书.

7.推送

PushMeBady是一款用来测试的APNs的开源Mac项目.
github下载地址:https://github.com/stefanhafeneger/PushMeBaby

8.APNS推送整体流程

.创建推送相关证书..在启动AppDelegate方法中调用注册远程通知
/*第2种app已经被关闭(进程已死)
启动app,启动完毕会调用AppDelegate的下面方法*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. //注册远程通知,iOS8.0以上的方法 UIUserNotificationSettings *settingNoti = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settingNoti];
[application registerForRemoteNotifications];//iOS7.0及以下的方法
// UIRemoteNotificationType type = UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;
// [application registerForRemoteNotificationTypes:type]; //进程已经被关闭(进程已死,再进来),获取远程通知内容
UILocalNotification *location = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (location) { NSLog(@"%@",location.userInfo);
}else{
NSLog(@"%@",location.userInfo);
} return YES;
}
//注册成功后会调用AppDelegate的下面方法,得到设备的deviceToken- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSLog(@"获取deviceToken:%@",deviceToken);}
/*当设备接收到远程推送通知时
如果程序是处于关闭状态,系统会在给用户展示远程推送通知的同时,将程序启动到后台,并调用AppDelegate的下面方法*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
if (userInfo) {
NSDictionary *aps = userInfo[@"aps"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"新的消息" message: [NSString stringWithFormat:@"%@",aps[@"alert"]] delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil];
[alert show]; }
}
/*当用户点击远程推送通知,会自动打开app,这里有2种情况
第1种:app并没有关闭,一直隐藏在后台
让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(UILocalNotification *)notification{ NSLog(@"remoteNotification=%@",notification);
}
/*可以在这个方法中做些数据下载操作,争取在用户点击通知前,就将数据下载完毕下载完毕要调用completionHandler这个block,告知下载完毕
completionHandler(UIBackgroundFetchResultNewData);*/
.下载PushMeBadyDemo,测试远程通知//注意使用PushMeBady时,在ioSock.h文件中把不必要的错误注释了如下面所示.
//#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
//电脑上导入开发者或发布者推送证书.修改名字为apns.cer//这里填写的获取的DeviceToken
self.deviceToken = @"e3094258 d8766f77 f29dfbfa ec3e3e83 8f0c213e 01e0eeea cc040536 e7d41730"; //这里是发布远程通知内容
self.payload = @"{\"aps\":{\"alert\":\"星仔爱善良的女孩.\",\"badge\":1}}";
//这里是远程通知APNs发布证书
self.certificate = [[NSBundle mainBundle] pathForResource:@"apns" ofType:@"cer"];

二. 极光推送(JPush)

1.什么是极光推送(JPush)

.一套远程推送解决方案,支持android和iOS两个平台
.它能够快捷地为iOS App增加推送功能,减少集成APNs需要的工作量、开发复杂度

2.步骤如下:

iOS远程推送11.注册账号,创建应用.

iOS远程推送12.填写应用信息,开发证书和生产证书密码是你导出证书生成p12文件的密码.

3.导出开发和发布(生产)推送证书,然后设置密码

iOS远程推送1

4.JPush会自动记录和管理所有安装过此app的设备deviceToken,利用JPush,可以轻易地给所有设备发送远程推送通知.

iOS远程推送1

未完待续………….

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727