极光推送

来源:互联网 发布:西科在线网络教育 编辑:程序博客网 时间:2024/04/30 05:06

因为项目需求要使用推送,而在极光官网上只是有配置环境,很少有获得内容等一些内容,我在网上找了一些大牛的博客,整理了一下。

在极光官网中下载的开发包最主要的是libs包里的的.jar和各种的os文件复制进去就行了,如果想使用极光的一些样式可以把res包中的文件放到相应的文件夹下,不过不用那些文件也是可以的,下面的代码我只是复制了libs文件的东西,整个文件就只是简单的默认的两个默认文件,和一个自定义的Servier文件

功能配置文件

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.example.service"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="19"        android:targetSdkVersion="19" />    <permission        android:name="你的包名.permission.JPUSH_MESSAGE"        android:protectionLevel="signature" />    <!-- Required  一些系统要求的权限,如访问网络等 -->    <uses-permission android:name="你的包名.permission.JPUSH_MESSAGE" />    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WAKE_LOCK" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_SETTINGS" />    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />    <!-- Optional for location -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!-- Required SDK 核心功能 -->        <!-- option since 2.0.5 可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process参数 将JPush相关组件设置为一个独立进程 -->        <!-- 如:android:process=":remote" -->        <service            android:name="cn.jpush.android.service.PushService"            android:enabled="true"            android:exported="false" >            <intent-filter>                <action android:name="cn.jpush.android.intent.REGISTER" />                <action android:name="cn.jpush.android.intent.REPORT" />                <action android:name="cn.jpush.android.intent.PushService" />                <action android:name="cn.jpush.android.intent.PUSH_TIME" />            </intent-filter>        </service>        <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 -->        <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 -->        <service            android:name="cn.jpush.android.service.DaemonService"            android:enabled="true"            android:exported="true" >            <intent-filter>                <action android:name="cn.jpush.android.intent.DaemonService" />                <category android:name="你的包名" />            </intent-filter>        </service>        <!-- Required -->        <receiver            android:name="cn.jpush.android.service.PushReceiver"            android:enabled="true" >            <intent-filter android:priority="1000" >                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />                <category android:name="你的包名" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.USER_PRESENT" />                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />            </intent-filter>            <!-- Optional -->            <intent-filter>                <action android:name="android.intent.action.PACKAGE_ADDED" />                <action android:name="android.intent.action.PACKAGE_REMOVED" />                <data android:scheme="package" />            </intent-filter>        </receiver>        <!-- Required SDK核心功能 -->        <activity            android:name="cn.jpush.android.ui.PushActivity"            android:configChanges="orientation|keyboardHidden"            android:exported="false" >            <intent-filter>                <action android:name="cn.jpush.android.ui.PushActivity" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="你的包名" />            </intent-filter>        </activity>        <!-- Required SDK核心功能 -->        <service            android:name="cn.jpush.android.service.DownloadService"            android:enabled="true"            android:exported="false" >        </service>        <!-- Required SDK核心功能 -->        <receiver android:name="cn.jpush.android.service.AlarmReceiver" />        <!-- User defined. 用户自定义的广播接收器 -->        <receiver            android:name="要继承BroadcastReceiver和实现onReceive广播接收的类"            android:enabled="true" >            <intent-filter>                <!-- Required 用户注册SDK的intent -->                <action android:name="cn.jpush.android.intent.REGISTRATION" />                <!-- Required 用户接收SDK消息的intent -->                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />                <!-- Required 用户接收SDK通知栏信息的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />                <!-- Required 用户打开自定义通知栏的intent -->                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />                <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />                <!-- 接收网络变化 连接/断开 since 1.6.3 -->                <action android:name="cn.jpush.android.intent.CONNECTION" />                <category android:name="你的包名" />            </intent-filter>        </receiver>        <!-- Required. For publish channel feature -->        <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。 -->        <!-- 例如: -->        <!-- 发到 Google Play 的APK可以设置为 google-play; -->        <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->        <!-- 目前这个渠道统计功能的报表还未开放。 -->        <meta-data            android:name="JPUSH_CHANNEL"            android:value="developer-default" />        <!-- Required. AppKey copied from Portal -->        <meta-data            android:name="JPUSH_APPKEY"            android:value="5fe99572d9ff7c5aaaeeea8e" />    </application></manifest>
上面是主要的权限和一些配置

下面是自定义的Servier类主要是继承BroadcastReceiver而实现onReceive方法用来接收极光推送过来的广播,其中有获取一些内容可以根据一些内容做一些操作
<pre name="code" class="java">public class Servier extends BroadcastReceiver {private static final String TAG = "JPush";@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubBundle bundle = intent.getExtras();Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction()+ ", extras: " + printBundle(bundle));if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {Log.d(TAG,"[MyReceiver] 接收到推送下来的自定义消息: "+ bundle.getString(JPushInterface.EXTRA_MESSAGE));receivingNotification(context, bundle);} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {Log.d(TAG, "[MyReceiver] 用户点击打开了通知");// 打开自定义的ActivityIntent i = new Intent(context, MainActivity.class);i.putExtras(bundle);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);context.startActivity(i);} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {Log.d(TAG,"[MyReceiver] 用户收到到RICH PUSH CALLBACK: "+ bundle.getString(JPushInterface.EXTRA_EXTRA));// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity,// 打开一个网页等..} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);Log.w(TAG, "[MyReceiver]" + intent.getAction()+ " connected state change to " + connected);} else {Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());}}// 通知栏的现实样式private void receivingNotification(Context context, Bundle bundle) {NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);// 使用notification// 使用广播或者通知进行内容的显示NotificationCompat.Builder builder = new NotificationCompat.Builder(context);builder.setContentText(message).setSmallIcon(R.drawable.ic_launcher).setContentTitle(JPushInterface.EXTRA_TITLE);builder.setDefaults(Notification.DEFAULT_SOUND);manager.notify(1, builder.build());}// 打印所有的 intent extra 数据@SuppressLint("NewApi")private static String printBundle(Bundle bundle) {StringBuilder sb = new StringBuilder();for (String key : bundle.keySet()) {if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {sb.append("\nkey0:" + key + ", value:" + bundle.getInt(key));} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {sb.append("\nkey1:" + key + ", value:" + bundle.getBoolean(key));} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {Log.i(TAG, "This message has no Extra data");continue;}try {JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));Iterator<String> it = json.keys();while (it.hasNext()) {String myKey = it.next().toString();sb.append("\nkey2:" + key + ", value: [" + myKey+ " - " + json.optString(myKey) + "]");}} catch (JSONException e) {Log.e(TAG, "Get message extra JSON error!");}} else {sb.append("\nkey3:" + key + ", value:" + bundle.getString(key));if (key.equals(JPushInterface.EXTRA_NOTIFICATION_TITLE)) {sb.append("\nkey4:" + key + ", value:"+ bundle.getString(key));if ((bundle.getString(key)).equals("网络订单")) {sb.append("\nkey5:" + key + ", value:"+ bundle.getString(key));type = true;sb.append("用户标题为网络订单:type已更改为:" + type);}}}}return sb.toString();}
其中的key3所在else中可以是根据JPushInterface.xxxxxxxxxxx获得标题和内容,其中的JPushInterface.EXTRA_NOTIFICATION_TITLE就是获得标题的内容,还有一些获得内容的操作可以在日志里注意一下key3打印出的内容



0 0