您的位置:首页 > 其它

JPush极光推送

2015-09-26 17:17 246 查看
点击打开链接,下载代码。。。









注册账号--创建工程--下载SDK


SDK集成步骤


1、导入 SDK 开发包到你自己的应用程序项目

解压缩 jpush-sdk_v1.x.y.zip 集成压缩包
复制 libs/jpush-sdk-release1.x.y.jar 到工程 libs/ 目录下
复制 libs/armeabi/libjpush1xy.so 到工程 libs/armeabi 目录下
复制 libs/armeabi-v7a/libjpush.so 到工程 libs/armeabi-v7a 目录下

如果您的项目有 libs/armeabi-v7a 这个目录,请把 libs/armeabi-v7a下的so文件 也复制一份到这个目录。

复制 res/drawable-hdpi 中的资源文件到工程的 res/drawable-hdpi/ 目录下
复制 res/layout 中的布局文件到工程的 res/layout/ 目录下


2、配置 AndroidManifest.xml

根据 SDK 压缩包里的 AndroidManifest.xml 样例文件,来配置应用程序项目的 AndroidManifest.xml 。

主要步骤为:

复制备注为 "Required" 的部分
将备注为替换包名的部分,替换为当前应用程序的包名
将AppKey替换为在Portal上注册该应用的的Key,例如(9fed5bcb7b9b87413678c407)

AndroidManifest.xml权限配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="您应用的包名"
android:versionCode="100"
android:versionName="1.0.0"
>
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />

<!-- Required -->
<permission android:name="您应用的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />

<!-- Required -->
<uses-permission android:name="You Package.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.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.WRITE_SETTINGS" />

<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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" />

应用包名及appkey替换:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="Your Application">

<!-- Required -->
<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>

<!-- Required SDK 核心功能-->
<!-- since 1.8.0 -->
<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="您自己定义的Receiver"
android:enabled="true">
<intent-filter>
<!--Required 用户注册SDK的intent-->
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.UNREGISTRATION" />
<!--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="Your AppKey"/>
</application>
</manifest>


以下代码定制一个本应用程序 Application 类。需要在 AndoridManifest.xml 里配置。请参考上面 AndroidManifest.xml 片断,或者 example 项目。
public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}


————————————————————————————项目代码————————————————————————————
MainActivity

package com.example.jpushdemo;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyApplication application = (MyApplication) getApplication();
}

}


MyApplication

package com.example.jpushdemo;

import android.app.Application;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
Log.e("TAG", "xxxx");

JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}

}


MyReceiver

package com.example.jpushdemo;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
}else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
System.out.println("收到了通知");
// 在这里可以做些统计,或者做些其他工作
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
System.out.println("用户点击打开了通知");

String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
System.out.println("附加信息:" + extra);

try {
JSONObject jo = new JSONObject(extra);
String url = jo.getString("url");

System.out.println("url:" + url);
// 跳浏览器加载网页
} catch (JSONException e) {
e.printStackTrace();
}

} else {
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: