android push notification serfice android中的push机制

APNS 是什么?

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

特点:

  • 快速集成:提供一种比C2DM更加快捷的使用方式,避免各种限制.
  • 无需架设服务器:通过使用"云服务",减少额外服务器负担.
  • 可以同时推送消息到网站页面,android 手机
  • 耗电少,占用流量少.
  • http://zen-mobi.com/get_api_key.php获取apikey

    如何在 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 Id 以及 此设备的标识 (chanel中唯一表示此设备的字符串) 传递过去:       Intent intent = new Intent(APNService.START);     intent.putExtra("ch", chanel);     intent.putExtra("devId", devId);     startService(intent); 
      NotesChanel Id 在申请 API 后,登录开发者页面会看到. devId: chanel 内设备标识,要在chanel内保持唯一.
    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 到设备

    通过 rest 接口发送 Notification:
    http://www.push-notification.org/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)


    更多信息请访问 java教程网www.itchm.com

    更多相关文章

    1. Android(安卓)无法接收开机广播的问题
    2. Android(安卓)kotlin使用注解实现防按钮连点功能
    3. Android(安卓)Studio 使用NDK编译时常见的问题
    4. android开机自启动的后台Service的实现 .
    5. Android自定义属性,format详解
    6. Android(安卓)Studio 插件
    7. 箭头函数的基础使用
    8. NPM 和webpack 的基础使用
    9. Python list sort方法的具体使用

    随机推荐

    1. Android基础知识学习——界面转换
    2. android studio设置自动导入
    3. 【android】Sax解析半个例子 :startElemen
    4. Android IntentService 深入分析
    5. JNI基本数据类型转换
    6. android ScrollView 多张图片之间有空白
    7. Android 获取设备唯一标识
    8. Android 原始下载图片 通过HTTp
    9. Android样式研究
    10. android监听edittext输入事件