1.百度上创建开发者帐号
http://developer.baidu.com/
2.登录后进入“开发者服务管理”
3.创建应用
APP KEY 需要记下来,cordova 程序中需要使用它。
4.创建cordova 应用
D:\app\android_workspace>cordova create TestPush com.app.push TestPush
Creating a new cordova project with name "TestPush" and id "com.app.push" at location "D:\app\android_workspace\TestPush"
5. 添加应用平台
D:\app\android_workspace>cd TestPush
D:\app\android_workspace\TestPush>cordova platform add android
Adding android project...
Creating Cordova project for the Android platform:
Path: platforms\android
Package: com.app.push
Name: TestPush
Android target: android-21
Copying template files...
Project successfully created.
6.添加百度云消息推送插件:
D:\app\android_workspace\TestPush>cordova plugin add com.cmpsoft.mobile.plugin.p
ushnotification
Fetching plugin "com.cmpsoft.mobile.plugin.pushnotification" via plugin registry
Installing "com.cmpsoft.mobile.plugin.pushnotification" for android
7.修改 项目根下 www/index.html ,添加如下:
<script type="text/javascript" > function onDeviceReady() { fastgoPushNotification.init("KerdAAt9KahoeC5BnVjtbiNX");//参数为百度云中创建应用的APP KEY } document.addEventListener('deviceready',onDeviceReady); </script>
8.启动应用测试
D:\app\android_workspace\TestPush>cordova run android
//使用android 手机测试,需要安装手机驱动,可以使用 adb shell 命令查看能否成功连接到手机
//省略N多输出
BUILD SUCCESSFUL
Total time: 43 seconds
Built the following apk(s):
D:\app\android_workspace\TestPush\platforms\android\ant-build\MainActivity-d
ebug.apk
Using apk: D:\app\android_workspace\TestPush\platforms\android\ant-build\MainAct
ivity-debug.apk
Installing app on device...
Launching application...
LAUNCH SUCCESS
这时应用已经启动了。
9.使用百度云推送消息
进入后选择创建通知:
点击发送。
10.手机端接收到消息