首页 > 未分类 > 如何在 Android 应用中使用 Notification ?
2011十一月22

如何在 Android 应用中使用 Notification ?

a) 在应用中添加 APNS 功能

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

  4. 接收 push notification
  5. 使用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, 处理收到的消息
    		 }
    	}
    }
  6. 启动 Push Notification Service
  7. 发送Intent 启动服务,将 chanel 名字以及 此设备的标识 (chanel中唯一表示此设备的字符串) 传递过去:
    
        Intent intent = new Intent(APNService.START);
        intent.putExtra("ch", chanel);
        intent.putExtra("devId", devId);
        startService(intent);
  8. 配置 AndroidManifest.xml
  9. ...
    <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 到设备

通过 Rect 接口发送 Notification:
http://www.push-notification.org/apns_v1.php?ch=YourChannelId&devId=YourCllientId&msg =”hello world”&random=0123&hash=HashCode

发送接口

通过 rest 接口发送通知到设备:
http://www.push-notification.org/handlers/apns_v1.php?ch=YourChannelId&devId=YourDevId&msg=hello world&random=0123&hash=HashCode

    参数:
	    ch:     api 的 channel Id.
	    devId:  接收设备Id.
	    msg:    要发送的消息.
	    random: 随机数
	    hash:  ch + devId + msg + random + apiKey 的MD5 校验码.

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

文章作者:admin
本文地址:http://www.zmkm.info/archives/203
版权所有 © 转载时必须以链接形式注明作者和原始出处!

本文目前尚无任何评论.

发表评论