device token

2024-06-06 14:18
文章标签 device token

本文主要是介绍device token,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



两种通知都是为了提醒用户后台执行的应用有了变化。从用户角度来看,效果是一样的,都是通知。只是实现的方式不一样,对于技术实现来说。

本文主要说明push notification的device token的步骤。

可以通过《偷窥iPhone Push Notification的幕后》和《iPhone的Push(推送通知)功能原理浅析》对push notification有个原理上的了解。


设备的准备

首先要知道,push notification只能在真机上运行的,无法在模拟器上使用,如果在模拟器上运行,在注册设备的时候会有类似如下报错:

Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5d249d0 {NSLocalizedDescription=remote notifications are not supported in the simulator}

真机也要注意,如果没有越狱,没有问题。越狱的话,比如通过blacksnOw,因为没有经过iTunes,无法生成有效的设备证书(device certificate),因此注册的时候不会成功。

检查越狱版本是否可用,可以ssh到设备上,执行命令:

ls /var/mobile/Library/Preferences/com.apple.apsd.plist  -l

-rw——- 1 mobile mobile 119 Aug 24 19:21 /var/mobile/Library/Preferences/com.apple.apsd.plist

返回的文件大小是119,就没有问题。

获取device token的原理

在说操作步骤之前,先说一下获取device token的一些原理方面的事情。

device token,即设备令牌,不是系统唯一标识(见获取iOS设备的基本信息),需要在应用启动时发起到apple服务器请求,注册自己的设备和应用,并获得这个device token。

device token有什么用?如果应用需要push notification给手机,那么它要有个服务器端(provider),但是它发出的信息不是直接给手机的,而是必须统一交给apple的服务器,这个服务器就是apple push notification server(apns)。apple服务器通过这个token,知道应用要发的消息是给哪个手机设备的,并转发该消息给手机,手机再通知应用程序。

获取device token的操作步骤

这里主要参照了这篇文章:Programming Apple Push Notification Services

该文档很详细,照做就应该没有问题。

需要注意的是identifier一定要和provision portal profile中的app id一致,即:

image

要和:

image

一致。

另外,要确保设备绑定的是唯一的profile:

image

编写代码,是在AppDelegate中增加两个方法:

  • didRegisterForRemoteNotificationsWithDeviceToken:当应用第一次运行的时候,ios获取到device token后调用,用于注册设备到apns上之后的操作(比如将device token通知应用的服务器端provider)
  • didFailToRegisterForRemoteNotificationsWithError:如果注册的时候失败,ios会调用这个方法,可以打印一些报错日志或者提醒用户通知不可用

另外,有一个方法需要增加内容,主要是打印日志,说明是否已经注册:

- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];      
    NSLog(@"Initiating remoteNoticationssAreActive");      
    if(!application.enabledRemoteNotificationTypes){      
        NSLog(@"Initiating remoteNoticationssAreActive1");      
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];      
    }      
    UIApplication* myapp = [UIApplication sharedApplication];      
    myapp.idleTimerDisabled = YES;      
    [window addSubview:viewController.view];      
    [window makeKeyAndVisible];      
}

第一次运行带注册方法的应用,会看到类似这样的提示窗口:

J%ZIE~[N)`A%S_MRQRS6I92

然后,在日志中看到类似下面的日志,主要是看到打印出device token的64位字符串,就说明成功了。

 

 

 

 

 

 

最近两天在研究ios的消息推送机制。研究这个东西,还是充满兴趣的。

Push的原理:

Push 的工作机制可以简单的概括为下图

图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider。
APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。

上图可以分为三个阶段。

第一阶段:.net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。
第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

    从上图我们可以看到。

   1、首先是应用程序注册消息推送。

   2、 IOS跟APNS Server要deviceToken。应用程序接受deviceToken。

   3、应用程序将deviceToken发送给PUSH服务端程序。

   4、 服务端程序向APNS服务发送消息。

   5、APNS服务将消息发送给iPhone应用程序。

无论是iPhone客户端跟APNS,还是Provider和APNS都需要通过证书进行连接的。下面我介绍一下几种用到的证书。

几种证书:

一、*.certSigningRequest文件

   1、生成Certificate Signing Request (CSR):

2、填写你的邮箱和Common Name,这里填写为PushChat。选择保存到硬盘。

这样就在本地生成了一个PushChat.certSigningRequest文件。

二、生成*.p12文件

1、导出密钥,并输入你的密码。

输入你的密码:

这样就生成了一个PushChatKey.p12文件。

三、新建一个App ID 和SSL certificate文件

1、用你的付过费的apple帐号登录到iOS Provisioning Portal。新建一个App ID。

     Description:中输入PushChat

     Bundle Seed ID:默认选择Generate New

     Bundle Identifier:输入com.mysoft.PushChat

    点击提交

这样就会生成下面这条记录:

点击配置:

出现下面界面,点击继续:

这里我们选择前面生成好的PushChat.certSigningRequest文件,点击生成。

正在生成

生成完毕,我们把它下载下来。命名为aps_developer_identity.cer。

点击完成,你会发现状态变成Enabled。

到现在为止,我们已经生成了3个文件。

1、PushChat.certSigningRequest

2、PushChatKey.p12

3、aps_developer_identity.cer

现在我们创建一个简单的iPhone应用程序。

1、打开Xcode,选择创建一个View-based Application。命名如下图:

2、在PushChatAppDelegate中的didFinishLaunchingWithOptions方法中加入下面代码:

  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {self.window.rootViewController = self.viewController;[self.window makeKeyAndVisible];// Let the device know we want to receive push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];return YES; }

通过registerForRemoteNotificationTypes方法,告诉应用程序,能接受push来的通知。

3、在xcode中运行,会弹出下面的提示框:

选择OK。表示此应用程序开启消息通知服务。

在 PushChatAppDelegate.m代码中添加下面方法获取deviceToken

  
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {NSLog(@"My token is: %@", deviceToken); }- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {NSLog(@"Failed to get token, error: %@", error); }

    获取到的deviceToken,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。

    发送通知的.net应用程序出来需要知道deviceToken之外,还需要一个与APNS连接的证书。

    这个证书可以通过我们前面生成的两个文件中得到。

使用OpenSSL

1、将aps_developer_identity.cer转换成 aps_developer_identity.pem格式。

openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM

2、将p12格式的私钥转换成pem,需要设置4次密码,密码都设置为:abc123。

openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChat.p12

3、用certificate和the key 创建PKCS#12格式的文件。

openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12

这样我们就得到了在.net应用程序中使用的证书文件:aps_developer_identity.p12

在.net应用程序中发送通知。

有个开源的类库:apns-sharp。

地址是:http://code.google.com/p/apns-sharp/

我们下载源代码,对里面的JdSoft.Apple.Apns.Notifications做相应的调整就能用了。

我们根据DeviceToken和p12File对JdSoft.Apple.Apns.Notifications.Test做相应的调整,如下图。

这样就OK了。

效果:

通知的代码:

  
for (int i = 1; i <= count; i++){//Create a new notification to send Notification alertNotification = new Notification(testDeviceToken);alertNotification.Payload.Alert.Body = string.Format("Testing {0}...", i);alertNotification.Payload.Sound = "default";alertNotification.Payload.Badge = i;//Queue the notification to be sent if (service.QueueNotification(alertNotification))Console.WriteLine("Notification Queued!");elseConsole.WriteLine("Notification Failed to be Queued!");//Sleep in between each message if (i < count){Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification...");System.Threading.Thread.Sleep(sleepBetweenNotifications);}}

用手机拍的ipad上面的显示:

总结:这篇文章主要是详细的讲述了ios消息推送机制的实现,如何通过.net应用程序发送 消息给ios应用程序。

这篇关于device token的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1036342

相关文章

【Java】Hashmap不能用基本的数据类型 Dimensions expected after this token

http://moto0421.iteye.com/blog/1143777 今天试了一下HahsMap, 采用如下形似定义 (这个下面是用了csdn的一位同仁的文章,仅作为讲解参考,请见谅) HashMap<int,String> map=new HashMap<int,String>();  map.put(1,"a");  map.put(2,"b");  map.pu

SpringBoot + Redis实现token权限认证(附源码)

一、引言 登陆权限控制是每个系统都应必备的功能,实现方法也有好多种。下面使用Token认证来实现系统的权限访问。 功能描述: 用户登录成功后,后台返回一个token给调用者,同时自定义一个@AuthToken注解,被该注解标注的API请求都需要进行token效验,效验通过才可以正常访问,实现接口级的鉴权控制。同时token具有生命周期,在用户持续一段时间不进行操作的话,token则会过期,

rails中Can't verify CSRF token authenticity错误解决办法

在rails中 以客户端去访问服务器端 经常会终端出现 Can't verify CSRF token authenticity   这是由于客户端访问服务器端   rails会需要token验证 只需要在服务端对应的Controller中加入 skip_before_filter :verify_authenticity_token,:only => : funct

json--eval--VM1423:3 Uncaught SyntaxError: Invalid or unexpected token

VM1423:3 Uncaught SyntaxError: Invalid or unexpected token     感觉几天没写了。今天遇到个问题,Java后台返回json格式的数据,但是在用eval转换的时候 报错 js报错,Uncaught SyntaxError: Unexpected token }不知道哪里错,求解答! function processRespo

[驱动] 所有Device设备文件类型释义

#define FILE_DEVICE_BEEP 0x00000001 // 蜂鸣器设备#define FILE_DEVICE_CD_ROM 0x00000002 // CD光驱设备#define FILE_DEVICE_CD_ROM_FILE_SYSTEM 0x00000003 // CD光驱文件系统设备#define FILE_D

基于Token的WEB后台认证机制

在现代的Web应用中,基于Token的认证机制广泛应用于前后端分离的架构中。相较于传统的基于Session的认证方式,Token认证更适合分布式应用和跨平台访问。下面将详细介绍Token认证机制的原理及实现方法。  Token认证机制原理 Token(令牌)认证机制通常包括以下几个步骤: 1. 用户登录:用户通过提交用户名和密码请求登录。 2. 服务器验证:服务器验证用户提交的凭证,验证通过

杀疯了!PerfXCloud-AI大模型夏日狂欢来袭,向基石用户赠送 ∞ 亿Token!

【澎峰科技重磅消息】 在全球范围内大模型正逐渐成为强大的创新驱动力。在这个充满激情的夏日,PerfXCloud为开发者和企业带来了前所未有的福利: 1. 零成本亲密、深度体验大模型,提供大量示范案例。 2. 向基石用户赠送∞亿Token的激励计划。 3. 技术大咖召集的技术沙龙和大模型开发者群等你加入。 PerfXCloud致力于为用户提供极致便捷的一键部署体验,以及全面保护客户私有模型

Struts2 s:token/标签 防止表单多次提交

<span style="font-size:10px;">1、使用Struts2的表单标签,其中需要增加token标签。如下:……Java代码<%@ taglib uri="/struts-tags" prefix="s" %> …… <s:form action="page1" theme="simple"> <s:datetimepicker name="order.dat

Mac OS10.10.1环境下eclipse中的Android device chooser找不到真机的解决方案

最近遇到一个问题,在Mac OS10.10.1环境下eclipse中的Android device chooser找不到真机。 1、首先想到的是找万能的度娘啦,然后一个大神给了个解决方案,地址在此:http://blog.csdn.net/vieri_ch/article/details/40456433  有需要的朋友快去瞅瞅,有图有真相,两种方法我都试过了,都有用。 2、如果还不行的话你