第一步
下载极光推送SDK,下载地址https://docs.jiguang.cn/jpush/resources/
解压后进入libs将jar复制到项目libs文件夹下,main新建jniLibs文件夹注意与java平级,将要依赖的so库复制进去,也可同时放入libs下(注意jar右键Add As Library)
第二步
AndroidManifest清单文件配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld.jpushdemo">
<permission
android:name="com.helloworld.jpushdemo.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!-- 要求 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.helloworld.jpushdemo.permission.JPUSH_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.REAL_GET_TASKS" />
<application
android:name=".MyAppaction"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Required SDK 核心功能-->
<!-- 可配置android:process参数将PushService放在其他进程中 -->
<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 3.0.9 Required SDK 核心功能-->
<provider
android:authorities="com.helloworld.jpushdemo.DataProvider"
android:name="cn.jpush.android.service.DataProvider"
android:exported="true" />
<!-- 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="com.helloworld.jpushdemo"/>
</intent-filter>
</service>
<!-- Required SDK核心功能-->
<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="com.helloworld.jpushdemo"/>
</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:theme="@android:style/Theme.NoTitleBar"
android:exported="false" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.helloworld.jpushdemo" />
</intent-filter>
</activity>
<!-- Required SDK核心功能-->
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<!-- Required since 3.0.7 -->
<!-- 新的tag/alias接口结果返回需要开发者配置一个自定的广播 -->
<!-- 该广播需要继承JPush提供的JPushMessageReceiver类, 并如下新增一个 Intent-Filter -->
<receiver
android:name=".MyJPushMessageReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="com.helloworld.jpushdemo" />
</intent-filter>
</receiver>
<!-- User defined. 用户自定义的广播接收器-->
<receiver
android:name=".MyReceiver"
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" />
<!-- 接收网络变化 连接/断开 since 1.6.3 -->
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.helloworld.jpushdemo" />
</intent-filter>
</receiver>
<!-- 设置 AppKey -->
<meta-data android:name="JPUSH_APPKEY" android:value="b6fe630e2bce584839547ce0"/>
</application>
</manifest>
第二步
/** * JPush自定义接收器 */ public class PushReceiver 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())) { String message = bundle.getString(JPushInterface.EXTRA_EXTRA); LogUtil.d("json=" + message); Gson gson = new Gson(); PayResultPushResponse payResultPushResponse = gson.fromJson(message, PayResultPushResponse.class); if (payResultPushResponse != null && StringUtil.isNotBlank(payResultPushResponse.getTradeType())) { // 跳转到支付结果页 Intent intent1 = new Intent(context, CodePayResultActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent1.putExtra(CodePayResultActivity.EXTRA_PAY_RESULT, (Serializable) payResultPushResponse); context.startActivity(intent1); } } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { System.out.println("收到了通知"); // 在这里可以做些统计,或者做些其他工作 String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE); if (UserSaveDataGlobal.getLoginState()) {//判断是否登录 Intent intent1 = new Intent(); intent1.setAction("com.smk.changeinfo"); intent1.putExtra("title", title); context.sendBroadcast(intent1); } } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { if (UserSaveDataGlobal.getLoginState()) {//判断是否登录 Gson gson = new Gson(); String message = bundle.getString(JPushInterface.EXTRA_EXTRA); LogUtil.d("json=" + message); WebResponse webResponse = gson.fromJson(message, WebResponse.class); //判断是否有跳转url if (webResponse.getDestination() != null) { if (webResponse.getDestination().startsWith("http")) { Intent i = new Intent(); i.setClass(context, WebviewActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra("contents", webResponse.getDestination()); i.putExtra("showClose", true); context.startActivity(i); } else { if (webResponse.getDestination().contains("smkapp")) { Routers.open(context, webResponse.getDestination(), new RouterCallback() { @Override public void notFound(Context context, Uri uri) { //没有这个地址或者应用就跳首页 Intent i = new Intent(context, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } @Override public boolean beforeOpen(Context context, Uri uri) { return false; } @Override public void afterOpen(Context context, Uri uri) { } @Override public void error(Context context, Uri uri, Throwable e) { } }); } else { //没有这个地址或者应用就跳首页 Intent i = new Intent(context, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } } else { //没有跳转url就判断是否在后台运行,在后台就唤醒app,在前台就不做操作 if (!isForeground(context)) { ArouseAppInBackground(context); } } } else { //未登录就跳登录界面 Intent i = new Intent(context, CitizenCardLoginActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } } //判断应用是否在前台运行 private boolean isForeground(Context context) { if (context != null) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; String currentPackageName = cn.getPackageName(); if (!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) { return true; } return false; } return false; } //唤醒在后台的app private void ArouseAppInBackground(Context context) { //获取ActivityManager ActivityManager mAm = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); //获得当前运行的task List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100); for (ActivityManager.RunningTaskInfo rti : taskList) { //找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台 if (rti.topActivity.getPackageName().equals(context.getPackageName())) { try { Intent resultIntent = new Intent(context, Class.forName(rti.topActivity.getClassName())); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(resultIntent); } catch (ClassNotFoundException e) { e.printStackTrace(); } return; } } //若没有找到运行的task,用户结束了task或被系统释放,则重新启动mainactivity Intent resultIntent = new Intent(context, MainActivity.class); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(resultIntent); } }