使用android push notification service 实现即时通知

APNS ( Android Push Notification Service) 是一种在Android 系统上实现推送的一套服务.通过http接口,向APNS服务器发送一个URL请求后,消息即会推送给指定的设备.


使用

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

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


2) 将apns_beta_20110831.jar添加到工程.

在工程上右键打开“属性”,选择 “Java Build Path”, 在 Libraries 中选择 “Add External JARs”, 选择下载的 apns_beta_20110831.jar.


3) 接收 push notification.

设备在接收到通知的时候会发送一个广播,通过使用BroadcastReceiver 接收并处理收到的通知.

  1. package com.apns.demo;
  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import com.apns.APNService;
  9. public class MyBroadcastReceiver extends BroadcastReceiver {
  10.       @Override
  11.       public void onReceive(Context context, Intent intent) {
  12.            if(intent.getAction().equals(APNService.ON_NOTIFICATION)) {
  13.                String str = intent.getStringExtra("data");
  14.                //todo, 处理收到的消息
  15.           }
  16.       }
  17. }
复制代码

4) 启动服务
发送Intent 启动服务,将 chanel 名字以及 此设备的标识 (chanel中唯一表示此设备的字符串) 传递过去:
  1. Intent intent = new Intent(APNService.START);
  2. intent.putExtra("ch", chanel);
  3. intent.putExtra("devId", devId);
  4. startService(intent);
复制代码
notes: devId,标示此channel 中设备的唯一id, 可以是唯一用户名, uid, IMEI 等等.

5) 配置AndroidManifest.xml配置 Service/BroadcastReceiver,添加权限。
  1. <application android:icon="@drawable/icon"
  2. ...
  3. <service android:name="com.apns.APNService" android:label="APNS">
  4.        <intent-filter>
  5.               <action android:name="com.apns.APNService.START" />
  6.               <action android:name="com.apns.APNService.STOP" />
  7.               <category android:name="android.intent.category.DEFAULT"/>
  8.        </intent-filter>
  9. </service>
  10. <receiver android:name="MyBroadcastReceiver">
  11.         <intent-filter>
  12.             <action android:name="com.apnsd.APNService.NOTIFICATION" />
  13.         </intent-filter>
  14. </receiver>
  15.       ...
  16. </application>
  17. <uses-permission android:name="android.permission.INTERNET" />
  18. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
复制代码

推送API:
通过Rect 接口发送通知到设备(登录 www.push-notification.mobi 会有一个测试平台):
  1. http://www.push-notification.mobi/handlers/apns_v1.php?ch=YourChannelId&devId=YourDevId&msg=”hello
  2. world”&random=0123&hash=HashCode
复制代码
参数:
ch: api 的 channel Id.
devId: 接收设备的Id.
msg:要发送的消息.
random:一个随机数hash.
ch + g + msg + random + privateKey的MD5 校验码.

返回值:
服务器返回一 xml 格式字符串:
  1. <response result="0" msg="sended"/>
复制代码
result 值:
-1: 服务器连接失败
0: 发送成功
1: 无权发送
2: 权限被阻止
3: 设备不在线
12: chanel 过期
13: hash code 不一致
14: 参数不合法
15: 意外错误
attach example.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值