APNS 是什么?
APNS (Android Push Notification Service) 是一种在 android 上轻松实现 push notification 的功能的解决方案. 只需申请一个 API Key, 经过简单的步骤即可实现 push notification 的功能.

到官方主页申请免费API, 下载 apns_beta_20110831.jar


官方主页: www.push-notification.mobi


如何在 Android 应用中使用 Notification ?
a) 在应用中添加 APNS 功能

下载 libaray: com_apns.jar
将com_apns.jar添加到工程
在工程上右键打开“属性”,选择 “Java Build Path”, 在 Libraries 中选择 “Add External JARs”, 选择下载的 com_apns.jar.



接收 push notification
使用BroadcastReceiver接收系统广播:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(APNService.ON_NOTIFICATION)) {
String str = intent.getStringExtra("data");
//todo, 处理收到的消息
}
}
}
启动 Push Notification Service
发送Intent 启动服务,将 chanel Id 以及 此设备的标识 (chanel中唯一表示此设备的字符串) 传递过去:

Intent intent = new Intent(APNService.START);
intent.putExtra("ch", chanel);
intent.putExtra("devId", devId);
startService(intent);
Notes Chanel Id 在申请 API 后,登录开发者页面会看到. devId: chanel 内设备标识,要在chanel内保持唯一.
配置 AndroidManifest.xml
...
<application android:icon="@drawable/icon"
...
<service android:name="com.apns.APNSService" android:label="APNS">
<intent-filter>
<action android:name="com.apns.APNService.START" />
<action android:name="com.apns.APNService.STOP" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="com.apnsd.APNService.NOTIFICATION" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...

b) 发送 Notification 到设备

通过 rest 接口发送 Notification:
http://www.push-notification.mobi/handlers/apns_v1.php?ch=YourChannelId&devId=xxxxx&msg =hello world&random=0123&hash=HashCode

ch:Channel Id
devId:接收设备 Id
msg:消息
random:随机数
hash:md5(ch + devId + msg + random + apiKey)
Notes 申请API后,可登录开发者页面使用测试控制台进行API测试.

更多相关文章

  1. Android(安卓)给 app默认权限(不弹窗申请权限)
  2. Android(安卓)Audio Focus的应用(requestAudioFocus)
  3. Android获取设备唯一标识完美解决方案
  4. android手机客户端上传文件,java servlet服务器端接收并保存到服
  5. Android(安卓)Studio 3.0开始android Device Monitor弃用
  6. Android(安卓)MapView 申请apiKey
  7. Android(安卓)MapView 申请apiKey
  8. android 命令(adb shell)进入指定模拟器或设备
  9. webview开发-适配多分辨率的Android设备

随机推荐

  1. 【血泪】SDL终于移植成功,并且在模拟器上
  2. 关于Android资源文件中出现百分号的问题
  3. android侧滑菜单-DrawerLayout的基本使用
  4. android中include标签使用详解
  5. Mars《Android开发视频教程》全集下载(第
  6. 详细Android Studio + NDK范例
  7. 如何扩大一个view的touch和click响应区域
  8. HttpURLConnection(java.net.CookieManage
  9. Android如何判断手机卡是SIM卡或者USIM卡
  10. 简单介绍Android应用特色及详解四大组件