(转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送
在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送
1,在iOS开发者中心: iOS Provisioning Portal 创建一个AppID,如图:
2,生成iOS Push Service证书,如图:
按照上述提示操作:
回到iOS Provisioning Portal:
点击“Download”下载iOS Push Service证书文件,如图:
3,导入证书文件到keychain:双击即可
4,生成ck.pem
导出cert.p12:
导出key.p12:
得到这样两个文件:
接下来打开终端:
输入命令:openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
然后输入命令:openssl pkcs12 -nocerts -out key.pem -in key.p12
最后合并成一个ck.pem文件:
输入命令:cat cert.pem key.pem > ck.pem
得到一个ck.pem文件:
5, 生成并安装Profile文件:
6, 上传ck.pem到SAE, 如图:
7, 客户端程序:
设置profile:
在info.plist中设置Bundle identifier:
在合适的位置加入下面代码,将你的应用注册到消息中心:
- 1234567891011121314
- (
IBAction
)action:(
id
)sender {
//注册到消息中心:
[[
UIApplication
sharedApplication
]
registerForRemoteNotificationTypes
:(
UIRemoteNotificationTypeBadge
|
UIRemoteNotificationTypeSound
|
UIRemoteNotificationTypeAlert
|
UIRemoteNotificationTypeNewsstandContentAvailability)];
}
在AppDelegate中加入下面代码:
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
#pragma mark -
#pragma mark APNS
- (
void
)application:(
UIApplication
*)application
didRegisterForRemoteNotificationsWithDeviceToken
:(
NSData
*)deviceToken {
//获得 device token
NSLog(
@"deviceToken: %@"
, deviceToken);
/*
. . . . . .
在这里把deviceToken和用户信息发送到服务器端
. . . . . .
*/
//获得 唯一标示
NSLog(
@"uniqueIdentifier: %@"
, [[
UIDevice
currentDevice
]
uniqueIdentifier
]);
}
- (
void
)application:(
UIApplication
*)application
didFailToRegisterForRemoteNotificationsWithError
:(
NSError
*)error {
NSLog(
@"Error in registration. Error: %@"
, error);
}
- (
void
)application:(
UIApplication
*)application
didReceiveRemoteNotification
:(
NSDictionary
*)userInfo {
/*
收到消息自定义事件
*/
if
([[userInfo
objectForKey
:
@"aps"
]
objectForKey
:
@"alert"
] !=
nil
) {
UIAlertView
*alert = [[
UIAlertView
alloc
]
initWithTitle
:
@"通知"
message
:[[userInfo
objectForKey
:
@"aps"
]
objectForKey
:
@"alert"
]
delegate
:
self
cancelButtonTitle
:
@"确定"
otherButtonTitles
:nil
];
[alert
show
];
[alert
release
];
}
}
8, 服务器端程序:
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
/**
*
* 实例代码
*
* SaeAPNS调用方法详见API文档:http://apidoc.sinaapp.com/sae/SaeAPNS.html
*
* @author Bruce Chen
*
*/
header(
"Content-Type: text/html;charset=utf-8"
);
include_once
(
"saeapns.class.php"
);
/* int $cert_id 许可证序号(1-10)*/
$cert_id
= 1;
/* string $device_token 设备令牌 */
$device_token
=
'xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx'
;
/* string $message 消息内容 */
$message
=
date
(
'Y-m-d H:i:s'
) .
": \n"
.
'测试消息 from SAE'
;
/*
array $body 消息体(包括消息、提醒声音等等),格式请参考示例和{@link http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1 Apple官方文档}
*/
$body
=
array
(
'aps'
=>
array
(
'alert'
=>
$message
,
'badge'
=> 1,
'sound'
=>
'in.caf'
),
'others'
=>
array
()
);
//实例化SaeAPNS
$apns
=
new
SaeAPNS();
//推送消息
$result
=
$apns
->push(
$cert_id
,
$body
,
$device_token
);
if
(
$result
!== false) {
echo
'发送成功'
;
}
else
{
echo
'发送失败'
;
var_dump(
$apns
->errno(),
$apns
->errmsg());
}
9, 手机收到通知效果:
源码实例下载地址: http://vdisk.weibo.com/s/SbY2
转自:http://blog.csdn.net/wave_1102/article/details/7669152
(转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送的更多相关文章
- Provider Communication with Apple Push Notification Service
This chapter describes the interfaces that providers use for communication with Apple Push Notificat ...
- 陌陌架构分享 – Apple Push Notification Service
http://blog.latermoon.com/?p=878 先描述下基本概念,标准的iPhone应用是没有后台运行的,要实现实时推送消息到手机,需要借助Apple提供的APNS服务. iPhon ...
- (转)How to build an Apple Push Notification provider server (tutorial)
转自:https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/ ...
- (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
转自:http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2 Upda ...
- (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
转自:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 Upda ...
- (转)苹果推送通知服务教程 Apple Push Notification Services Tutorial
本文译自http://www.raywenderlich.com/.原文由iOS教程团队 Matthijs Hollemans 撰写,经原网站管理员授权本博翻译. 在iOS系统,考虑到手机电池电量,应 ...
- [Erlang 0106] Erlang实现Apple Push Notifications消息推送
我们的IOS移动应用要实现消息推送,告诉用户有多少条消息未读,类似下图的效果(笑果),特把APNS和Erlang相关解决方案笔记于此备忘. 上面图片中是Apple Notif ...
- Push:iOS基于APNS的消息推送
1. Push的三个步骤,如下图所示: (1)Push服务应用程序把要发送的消息.目的iPhone的标识打包,发给APNS: (2)APNS在自身的已注册Push服务的iPhone列表中,查找有相应标 ...
- [置顶] 手把手教你iOS消息推送证书生成以及Push消息
iOS推送消息是许多iOS应用都具备的功能,今天在给应用加推送功能,在生成证书的过程中,发生了各种令人蛋痛的事.下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provi ...
随机推荐
- DELL RACADM 批量升级戴尔IDRAC固件
需求:通过服务器远程管理IP批量升级戴尔IDRAC固件 工具:racadm.ipmitool.Remote Access Configuration Tool 下载: 第一步,将要更新BMC IP写入 ...
- LINQ Enumerable 续 II
Enumerable.TakeWhile和Enumerable.SkpWhile Enumerable.TakeWhile和Enumerable.SkpWhile将通过判断条件,来获取和跳过序列. T ...
- HTML5和CSS3实例教程[总结一]
关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...
- SQL语句添加删除修改字段及一些表与字段的基本操作
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200)2.删除字段 ALTER TABLE table_NA ...
- [!] Unable to satisfy the following requirements:
出现这个问题是由于我本地Podfile文件上第三方版本太低. 解决方案就是,更新一下本地Podfile文件上的第三方版本,也就是pod update --verbose一下. 注意一下,这个命令需要很 ...
- php代码的一些高效写法
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...
- d039: 点的位置
内容: 已知一平面直角坐标系中正方形的左上(-2,2)和右下(2,-2)的顶点坐标,,当给一个点的坐标,判断点和正方形的关系,在正方形内(含边上)输出True ,否则输出 False 输入说明: 一行 ...
- 在iOS开发中使用FMDB-备用
SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库.iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyli ...
- VHDL学习之TEXTIO在仿真中的应用
TEXTIO 在VHDL 仿真与磁盘文件之间架起了桥梁,使用文本文件扩展VHDL 的仿真功能.本文介绍TEXTIO 程序包,以一个加法器实例说明TEXTIO 的使用方法,最后使用ModelSim 对设 ...
- 移植rom移动TD到联通W
1.修改build.prop TD为 ril.flightmode.poweroffMD=0 ril.telephony.mode=2 改为 ril.flightmode.poweroffMD=1 r ...