极光推送作用:
极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率
主要作用:
保持与服务器的长连接,以便消息能够即时推送到达客户端
接收通知与自定义消息,并向开发者 App 传递相关信息
SDK 所支持的 Android 系统版本
目前 SDK 只支持 Android 2.3 或以上版本的手机系统;
富媒体信息流功能则需 Android 3.0 或以上版本的系统。
手动集成步骤:
手动集成压缩包下载链接:http://docs.jiguang.cn/jpush/resources/
- 解压缩 jpush-android–3.x.x-release.zip 集成压缩包。
- 复制 libs/jcore-android-x.x.x.jar 到工程 libs/ 目录下。
- 复制 libs/jpush-android-3.x.x.jar 到工程 libs/ 目录下。
- 复制 libs/(cpu-type)/libjcore1xy.so 到你的工程中存放对应 cpu 类型的目录下。
- 复制 res/ 中 drawable-hdpi, layout, values 文件夹中的资源文件到你的工程中 res/ 对应同名的目录下。
共用了极光一键登入的jcore,就没有复制jcore-android-x.x.x.jar到lib下
说明 1:若没有 res/drawable-xxxx/jpush_notification_icon 这个资源默认使用应用图标作为通知 icon,在 5.0 以上系统将应用图标作为 statusbar icon 可能显示不正常,用户可定义没有阴影和渐变色的 icon 替换这个文件,文件名不要变。
说明 2:使用 android studio 的开发者,如果使用 jniLibs 文件夹导入 so 文件,则仅需将所有 cpu 类型的文件夹拷进去;如果将 so 文件添加在 module的libs 文件夹下,注意在 module 的 gradle 配置中添加一下配置:
sourceSets { main { jniLibs.srcDirs = ['libs'] jniLibs.srcDirs = ['src/main/jniLibs'] assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
配置 AndroidManifest.xml
<receiver android:name=".message.JPushReceiver" android:enabled="true"> <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- <!– Required 用户注册SDK的intent –> --> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- <!– Required 用户接收SDK消息的intent –> --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- <!– Required 用户接收SDK通知栏信息的intent –> --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- <!– Required 用户打开自定义通知栏的intent –> --> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- <!– Optional 用户接受Rich Push Javascript 回调函数的intent –> --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- <!– JPush 服务的连接状态发生变化。(注:不是指 Android 系统的网络连接状态。) 连接/断开 since 1.6.3 –> --> <category android:name="**********" /> </intent-filter> </receiver> <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 --> <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 --> <!-- Rich push 核心功能 since 2.0.6 --> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:exported="true"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="cn.jpush.android.ui.PopWinActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- Required SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:exported="true" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- Required SDK 核心功能 --> <!-- 可配置android:process参数将PushService放在其他进程中 --> <service android:name="cn.jpush.android.service.PushService" android:exported="false" android:process=":pushcore"> <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:name="cn.jpush.android.service.DataProvider" android:authorities="${applicationId}.DataProvider" android:exported="false" android:process=":pushcore" /> <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 --> <!-- 若不启用该功能可删除该组件,或把 enabled 设置成 false ;App 不会被其他 App 拉起,但会拉起其他的 App。 --> <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="${applicationId}" /> </intent-filter> </service> <!-- 可选,如果使用静态Activity方式拉起,该组件必须声明 --> <activity android:name="cn.jpush.android.service.DActivity" android:enabled="true" android:exported="true" android:taskAffinity="jpush.custom" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.intent.DActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- since 3.1.0 Required SDK 核心功能 --> <provider android:name="cn.jpush.android.service.DownloadProvider" android:authorities="${applicationId}.DownloadProvider" android:exported="true" /> <!-- 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" /> <!-- Required 显示通知栏 --> <category android:name="${applicationId}" /> </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核心功能 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false" /> <!-- <!– 3.5.0新增,用于定时展示功能 –> <receiver android:name="cn.jpush.android.service.SchedulerReceiver" android:exported="false"/> --> <receiver android:name=".message.PushMessageReceiver"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> <activity android:name="cn.jpush.android.service.JNotifyActivity" android:exported="false" android:taskAffinity="jpush.custom" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.intent.JNotifyActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity>
写自定义接受通知的类JPushReceiver
public class JPushReceiver extends BroadcastReceiver { //SDK 向 JPush Server 注册所得到的注册 ID public static final String EXTRA_REGISTRATION_ID = "cn.jpush.android.intent.REGISTRATION"; //收到了自定义消息 Push public static final String ACTION_MESSAGE_RECEIVED = "cn.jpush.android.intent.MESSAGE_RECEIVED"; //收到了通知 Push public static final String ACTION_NOTIFICATION_RECEIVED = "cn.jpush.android.intent.NOTIFICATION_RECEIVED"; //用户点击了通知。 一般情况下,用户不需要配置此 receiver action。 public static final String ACTION_NOTIFICATION_OPENED = "cn.jpush.android.intent.NOTIFICATION_OPENED"; //用户点击了通知栏中自定义的按钮。(SDK 3.0.0 以上版本支持) public static final String ACTION_NOTIFICATION_CLICK_ACTION = "cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION"; //JPush 服务的连接状态发生变化。(注:不是指 Android 系统的网络连接状态。) public static final String ACTION_CONNECTION_CHANGE = "cn.jpush.android.intent.CONNECTION"; private static final String TAG = "JIGUANG-Example"; private MsgEvent msgEvent; public static final String CHANNEL_ID = "**************"; private static final String CHANNEL_NAME = "Default Channel"; private static final String CHANNEL_DESCRIPTION = "this is default channel!"; private Context context; @Override public void onReceive(Context context, Intent intent) { this.context = context; switch (intent.getAction()) { case EXTRA_REGISTRATION_ID: //SDK 向 JPush Server 注册所得到的注册 ID break; case ACTION_MESSAGE_RECEIVED: break; case ACTION_NOTIFICATION_RECEIVED: //收到了自定义消息 Push // onMessageReceived(context,intent); BaseNetTasks.JPUST_UNREAD_COUNT++; //全局的消息参数加1 MsgEvent msgEvent = new MsgEvent(Constant.JPUST_NUM); //订阅事件处,显示全局的消息数 msgEvent.setData(BaseNetTasks.JPUST_UNREAD_COUNT); EventBusManager.post(msgEvent); //收到了通知 Push onNotificationReceived(context, intent); break; case ACTION_NOTIFICATION_OPENED: //用户点击了通知。 一般情况下,用户不需要配置此 receiver action。 //openNotification(context, intent); break; case ACTION_NOTIFICATION_CLICK_ACTION: //用户点击了通知栏中自定义的按钮。(SDK 3.0.0 以上版本支持) openNotification(context, intent); break; case ACTION_CONNECTION_CHANGE: //JPush 服务的连接状态发生变化。(注:不是指 Android 系统的网络连接状态。) break; } } //点击通知栏的处理 private void openNotification(Context context, Intent intent) { String extra = intent.getStringExtra(JPushInterface.EXTRA_EXTRA); NotifityContentBean bean = new Gson().fromJson(extra, NotifityContentBean.class); //NotifityContentBean根据后台发送的值自定义 Intent targetIntent; switch (bean.key) { case "2": targetIntent = new Intent(context, PublishTaskManageListActivity.class); break; case "10": targetIntent = new Intent(context, ReceiveTaskManageListActivity.class); break; case "33": targetIntent = new Intent(context, PublishTaskManageListActivity.class); targetIntent.putExtra(Constant.ORDER_NAME, "接单订单");//标题 break; . . . default: targetIntent = new Intent(context, MainActivity.class); } targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(targetIntent); } //收到通知 private void onNotificationReceived(Context context, Intent intent) { onMessageReceived(context, intent); } private void onMessageReceived(Context context, Intent intent) { alertMsg(context, intent); } private void alertMsg(Context context, Intent intent) { if (intent != null) { //获取收到的通知内容 String extra = intent.getStringExtra(JPushInterface.EXTRA_EXTRA); NotifityContentBean bean = new Gson().fromJson(extra, NotifityContentBean.class); String key = bean.key; String jpush_msg = bean.msg; String taskId = bean.taskId; String receiveId = bean.receiveId; int notifyId = (int) System.currentTimeMillis(); Intent clickIntent = new Intent(); clickIntent.putExtra(JPushInterface.EXTRA_EXTRA, extra); System.out.println("JPushInterface.EXTRA_EXTRA:" + extra); clickIntent.setAction(ACTION_NOTIFICATION_OPENED); PendingIntent contentIntent = PendingIntent.getBroadcast(context, notifyId, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); //重点:先创建通知渠道 (Android8.0及以上需要创建通知渠道) NotificationChannel mChannel = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); mChannel.setDescription(CHANNEL_DESCRIPTION); mChannel.setShowBadge(false); notificationManager.createNotificationChannel(mChannel); } //再创建通知 NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID); notification.setAutoCancel(true) .setOnlyAlertOnce(true) .setContentTitle("xxxxx") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(contentIntent); notification.setPriority(NotificationCompat.DEFAULT_ALL); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { if (mChannel != null) notification.setChannelId(mChannel.getId()); } notification.setContentText(jpush_msg); // 推送文本 if (!TextUtils.isEmpty(key)) { switch (key) { case "15"://顶号 notification.setSound(Uri.parse("")); OtherUtil.cleanInfo(context); Intent mIntent = new Intent(context, NewLoginActivity.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mIntent); APPActivityManager.getAppManager().finishActivityUntilMain(); return; . . . default: break; } } notificationManager.notify(notifyId, notification.build()); Log.d("pushMmessage", "key=" + key + " receiveId=" + receiveId + " taskId=" + taskId + " jpush_msg=" + jpush_msg); //弹窗显示通知 popWindow(context, jpush_msg, key, taskId, receiveId); } }
}
为什么要对接厂商通道:
在程序没有被杀死的时候,能收到通知;在程序被杀死的时候,通知栏不显示通知,接收消息的代码也没有运行,处理不了消息
厂商通道的好处:(没有对接厂商通道,是极光服务器直接发送给手机;对接了厂商通道,进程存活的时候,是极光服务器直接发送给手机,进程没有存活的时候,极光服务器先发送给厂商,厂商通过自己的通知推送推送到手机)
在国内 Android 生态中,推送通道都是由终端与云端之间的长链接来维持,严重依赖于应用进程的存活状态。如今一些手机厂家会在自家 rom 中做系统级别的推送通道,再由系统分发给各个 app,以此提高在自家 rom 上的推送送达率。
接入极光厂商步骤
1.在极光后台点击推送设置,选择集成设置,选择Android,在厂商名称里选择填写华为,小米,OPPO,VIVO的相关厂商参数(在各大平台的软件信息里有),选择开启厂商通道
2.按照极光文档对接,地址:http://docs.jiguang.cn/jpush/client/Android/huawei-Push-guide/
2.1 在根级 build.gradle 中添加规则
2.2 把third-push目录下libs中的需要的厂商jar拷贝其中的jar包至工程的libs目录下
2.3 配置小米推送sdk所需要的权限
<permission
android:name="您应用的包名.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="您应用的包名.permission.MIPUSH_RECEIVE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.VIBRATE" />
2.4配置组件和自定义服务(HWPushService华为,XMPushReceiver小米,OpenClickActivity 点击通知打开的Activity )
<activity android:name=".message.activitys.OpenClickActivity" android:exported="true" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <receiver android:name=".message.JPushReceiver" android:enabled="true"> <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- <!– Required 用户注册SDK的intent –> --> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- <!– Required 用户接收SDK消息的intent –> --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- <!– Required 用户接收SDK通知栏信息的intent –> --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- <!– Required 用户打开自定义通知栏的intent –> --> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- <!– Optional 用户接受Rich Push Javascript 回调函数的intent –> --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- <!– JPush 服务的连接状态发生变化。(注:不是指 Android 系统的网络连接状态。) 连接/断开 since 1.6.3 –> --> <category android:name="************" /> </intent-filter> </receiver> <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 --> <!-- 若不启用该功能可删除该组件,将不拉起其他应用也不能被其他应用拉起 --> <!-- Rich push 核心功能 since 2.0.6 --> <activity android:name="cn.jpush.android.ui.PopWinActivity" android:exported="true"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="cn.jpush.android.ui.PopWinActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- Required SDK核心功能 --> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:exported="true" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- Required SDK 核心功能 --> <!-- 可配置android:process参数将PushService放在其他进程中 --> <service android:name="cn.jpush.android.service.PushService" android:exported="false" android:process=":pushcore"> <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:name="cn.jpush.android.service.DataProvider" android:authorities="${applicationId}.DataProvider" android:exported="false" android:process=":pushcore" /> <!-- since 1.8.0 option 可选项。用于同一设备中不同应用的JPush服务相互拉起的功能。 --> <!-- 若不启用该功能可删除该组件,或把 enabled 设置成 false ;App 不会被其他 App 拉起,但会拉起其他的 App。 --> <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="${applicationId}" /> </intent-filter> </service> <!-- 可选,如果使用静态Activity方式拉起,该组件必须声明 --> <activity android:name="cn.jpush.android.service.DActivity" android:enabled="true" android:exported="true" android:taskAffinity="jpush.custom" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.intent.DActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- since 3.1.0 Required SDK 核心功能 --> <provider android:name="cn.jpush.android.service.DownloadProvider" android:authorities="${applicationId}.DownloadProvider" android:exported="true" /> <!-- 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" /> <!-- Required 显示通知栏 --> <category android:name="${applicationId}" /> </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核心功能 --> <receiver android:name="cn.jpush.android.service.AlarmReceiver" android:exported="false" /> <!-- <!– 3.5.0新增,用于定时展示功能 –> <receiver android:name="cn.jpush.android.service.SchedulerReceiver" android:exported="false"/> --> <receiver android:name=".message.PushMessageReceiver"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> <activity android:name="cn.jpush.android.service.JNotifyActivity" android:exported="false" android:taskAffinity="jpush.custom" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.intent.JNotifyActivity" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- 配置小米必须的组件 start --> <service android:name="com.xiaomi.push.service.XMJobService" android:enabled="true" android:exported="false" android:permission="android.permission.BIND_JOB_SERVICE" android:process=":pushservice" /> <service android:name="com.xiaomi.push.service.XMPushService" android:enabled="true" android:process=":pushservice" /> <service android:name="com.xiaomi.mipush.sdk.PushMessageHandler" android:enabled="true" android:exported="true" /> <service android:name="com.xiaomi.mipush.sdk.MessageHandleService" android:enabled="true" /> <receiver android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" android:exported="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiver android:name="com.xiaomi.push.service.receivers.PingReceiver" android:exported="false" android:process=":pushservice"> <intent-filter> <action android:name="com.xiaomi.push.PING_TIMER" /> </intent-filter> </receiver> <!-- 配置小米必须的组件 end --> <!-- 配置JPush接受的小米sdk的消息接受类 start --> <!-- <receiver android:name="cn.jpush.android.service.PluginXiaomiPlatformsReceiver" android:exported="true"> <intent-filter> <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" /> </intent-filter> </receiver> --> <receiver android:name=".message.XMPushReceiver" android:exported="true"> <intent-filter> <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /> </intent-filter> <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" /> </intent-filter> </receiver> <!-- 配置JPush接受的小米sdk的消息接受类 end --> <meta-data android:name="XIAOMI_APPKEY" android:value="MI-*********" /> <meta-data android:name="XIAOMI_APPID" android:value="MI-*********" /> <!-- <service android:name="cn.jpush.android.service.PluginHuaweiPlatformsService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT" /> </intent-filter> </service> --> <service android:name=".message.HWPushService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT" /> </intent-filter> </service> <receiver android:name="cn.jpush.android.service.PluginVivoMessageReceiver"> <intent-filter> <!-- 接收 push 消息 --> <action android:name="com.vivo.pushclient.action.RECEIVE" /> </intent-filter> </receiver> <service android:name="com.vivo.push.sdk.service.CommandClientService" android:exported="true" /> <activity android:name="com.vivo.push.sdk.LinkProxyClientActivity" android:exported="false" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <meta-data android:name="com.vivo.push.api_key" android:value="***********" /> <meta-data android:name="com.vivo.push.app_id" android:value="*********" /> <!-- since JPushv3.6.8 ,oppov2.1.0 oppo 核心功能 --> <service android:name="cn.jpush.android.service.PluginOppoPushService" android:permission="com.coloros.mcs.permission.SEND_MCS_MESSAGE"> <intent-filter> <action android:name="com.coloros.mcs.action.RECEIVE_MCS_MESSAGE" /> </intent-filter> </service> <!-- since JPushv3.6.8 ,oppov2.1.0 oppo 核心功能 --> <service android:name="com.heytap.msp.push.service.DataMessageCallbackService" android:permission="com.heytap.mcs.permission.SEND_PUSH_MESSAGE"> <intent-filter> <action android:name="com.heytap.mcs.action.RECEIVE_MCS_MESSAGE" /> <action android:name="com.heytap.msp.push.RECEIVE_MCS_MESSAGE" /> </intent-filter> </service> <!-- 兼容Q版本 --> <meta-data android:name="OPPO_APPKEY" android:value="OP-*********" /> <meta-data android:name="OPPO_APPID" android:value="OP-*********" /> <meta-data android:name="OPPO_APPSECRET" android:value="OP-*********" />
HWPushService类
public class HWPushService extends HmsMessageService { final PluginHuaweiPlatformsService service = new PluginHuaweiPlatformsService(); @Override public void onNewToken(String s) { service.onNewToken(s); Log.d("HWPushService","HWPushService "+s); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { service.onMessageReceived(remoteMessage); Log.d("HWPushService","onMessageReceived"); } @Override public void onMessageSent(String s) { service.onMessageSent(s); Log.d("HWPushService","onMessageSent"); } @Override public void onSendError(String s, Exception e) { service.onSendError(s,e); Log.d("HWPushService","onSendError"); } @Override public void onDeletedMessages() { service.onDeletedMessages(); Log.d("HWPushService","onDeletedMessages"); } }
XMPushReceiver类
public class XMPushReceiver extends PushMessageReceiver { final PluginXiaomiPlatformsReceiver receiver = new PluginXiaomiPlatformsReceiver(); @Override public void onReceivePassThroughMessage(final Context context, final MiPushMessage message) { Log.d("Jpush", "onReceivePassThroughMessage"); // receiver.onReceivePassThroughMessage(context, message); } @Override public void onNotificationMessageClicked(Context context, MiPushMessage message) { // receiver.onNotificationMessageClicked(context, message); Log.d("Jpush", "onNotificationMessageClicked"); } @Override public void onNotificationMessageArrived(Context context, MiPushMessage message) { Log.d("Jpush", "onNotificationMessageArrived" + " message=" + message.toString()); // receiver.onNotificationMessageArrived(context, message); // message(context,message); } @Override public void onCommandResult(Context context, MiPushCommandMessage message) { Log.d("Jpush", "onCommandResult"); // receiver.onCommandResult(context, message); } @Override public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) { // receiver.onReceiveRegisterResult(context, message); Log.d("Jpush", "onReceiveRegisterResult"); }
}
OpenClickActivity 点击通知打开的Activity
public class OpenClickActivity extends Activity { private static final String TAG = "OpenClickActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_open_click); Log.d(TAG, "用户点击打开了通知"); //handleOpenClick(); try { String data = null; //获取华为平台附带的jpush信息 if (getIntent().getData() != null) { data = getIntent().getData().toString(); } //获取fcm、oppo、vivo、华硕、小米平台附带的jpush信息 if (TextUtils.isEmpty(data) && getIntent().getExtras() != null) { data = getIntent().getExtras().getString("JMessageExtra"); } if (TextUtils.isEmpty(data)) return; //进程被被杀死,走厂商通道点击通知,会走到这里,先跳转到主页,在主页根据通知的类型,选择跳转的页面; //也可以在这里就判断需要跳转的页面 MainActivity.start(OpenClickActivity.this, data); finish(); } catch (Exception e) { e.printStackTrace(); //如果有错误,友盟上报错误信息到友盟后台 if (null != e) { MobclickAgent.reportError(OpenClickActivity.this, " errInfo:" + e.getLocalizedMessage()); } } }
2.5 在build.gradle中配置在华为后台添加的指纹证书对应的签名
signingConfigs {
release {
storeFile file("release.keystore")//签名文件的path
storePassword "123456"
keyAlias "android.keystore"
keyPassword "123456"
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug{
minifyEnabled false
signingConfig signingConfigs.release
}
}
2.6 混淆
2.6.1 华为
-ignorewarning
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}
如果开发者使用了AndResGuard,需要在混淆配置文件中加入AndResGuard白名单。
"R.string.hms*",
"R.string.connect_server_fail_prompt_toast",
"R.string.getting_message_fail_prompt_toast",
"R.string.no_available_network_prompt_toast",
"R.string.third_app_*",
"R.string.upsdk_*",
"R.layout.hms*",
"R.layout.upsdk_*",
"R.drawable.upsdk*",
"R.color.upsdk*",
"R.dimen.upsdk*",
"R.style.upsdk*",
"R.string.agc*"
2.6.2 小米
-dontwarn com.xiaomi.push.**
-keep class com.xiaomi.push.** { *; }
2.6.3 oppo
-dontwarn com.coloros.mcsdk.**
-keep class com.coloros.mcsdk.** { *; }
-dontwarn com.heytap.**
-keep class com.heytap.** { *; }
-dontwarn com.mcs.**
-keep class com.mcs.** { *; }
2.6.4 vivo
3 运行程序测试
3.1 进程存活的时候,走的是极光通道
3.2 杀死进程或者程序没有运行的时候,走的是厂商通道(极光先发送给厂商,厂商发送到具体的手机,在手机对应的平台的推送记录里可以看到)
这里手机是小米,在小米的推送记录里可以看到
杀死进程后,也能收到通知,通知栏也有通知显示