Module的build.gradle添加:
compile 'cn.jiguang:jpush:2.1.8' // 极光推送添加权限(不止这些权限,还有很多,具体一定要查看官方文档,这里已经去掉重复的):
<!--极光推送--> <!-- Required --> <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.WAKE_LOCK" />application节点里:
<!--极光推送--> <!-- 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=".receiver.JPushReceiver" 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" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- 接收网络变化 连接/断开 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="极光推送的APPKEY"/>广播接收者:
/** * Created by 詹子聪 on 2016/12/1. */ public class JPushReceiver 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 content = ""; String url = ""; String json = bundle.getString(JPushInterface.EXTRA_EXTRA); // 附加信息 //bundle.getString(JPushInterface.EXTRA_ALERT);// 通知内容 //bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);// 通知标题 try { JSONObject job = new JSONObject(json); content = job.optString("content"); url = job.optString("url"); } catch (JSONException e) { e.printStackTrace(); } if (TextUtils.isEmpty(url) && TextUtils.isEmpty(content)) { AppTool.openAppByPackageName(context, context.getPackageName()); } else { // 在这里可以自己写代码去定义用户点击后的行为,关闭接收推送的界面时再打开应用 Intent pushIntent = new Intent(context, JPushActivity.class); // 自定义打开的界面 pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);// 解决不能启动及启动之后点击通知没反应的bug //And for Pending intent use PendingIntent.FLAG_UPDATE_CURRENT pushIntent.putExtra("title", bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE)); pushIntent.putExtra("url", url); pushIntent.putExtra("content", content); context.startActivity(pushIntent); } } else { Log.d("JPush", "Unhandled intent - " + intent.getAction()); } } }接收通知的Activity:
public class JPushActivity extends BaseActivity { private String title; private String url; private String content; @Override public int bindLayout() { return R.layout.activity_jpush; } @Override public void initParams(Bundle parms) { title = parms.getString("title", "通知"); url = parms.getString("url", ""); content = parms.getString("content", ""); } @Override public void initView(View view) { //设置状态栏颜色 StatusBarUtil.setColor(JPushActivity.this, getResources().getColor(R.color.titlecolor), 0); //初始化标题栏 initBackTitleBar(title, Gravity.LEFT | Gravity.CENTER_VERTICAL, getResources().getColor(R.color.white), new View.OnClickListener() { @Override public void onClick(View v) { if (!ConstantUtil.isAppStarted) { AppTool.openAppByPackageName(JPushActivity.this, getPackageName()); } finish(); } }); TextView tv_content = (TextView) findViewById(R.id.tv_content); WebView wv_push = (WebView) findViewById(R.id.wv_push); if (!TextUtils.isEmpty(url)) { // 显示网站内容 tv_content.setVisibility(View.GONE); wv_push.setVisibility(View.VISIBLE); // 设置WebView属性,能够执行Javascript脚本 WebSettings settings = wv_push.getSettings(); settings.setJavaScriptEnabled(true); settings.setBuiltInZoomControls(true); // 设置显示完整网页 settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); settings.setDefaultTextEncodingName("UTF -8");//设置默认为utf-8 wv_push.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); wv_push.loadUrl(url); } else { // 显示文本 tv_content.setVisibility(View.VISIBLE); wv_push.setVisibility(View.GONE); tv_content.setText(content); } } @Override public void doBusiness(Context mContext) { } }application的onCreate里:
JPushInterface.init(this);