前提:你登录的JPush中上有你需要推送的应用(包名要与你项目的 “Your Package” 一致)
1、导包:
libs\jpush-sdk-release1.6.1.jar
libs\rmeabi\libjpush.so
2、在
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Your Package"
android:versionCode="100"
android:versionName="1.0.0"
>
<!-- Required -->
<permission android:name="com.itheima39.push.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />
<!-- Required -->
<uses-permission android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">.push.permission.JPUSH_MESSAGE" /></span>
<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.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!--since 1.6.0 -->
<!-- Optional. Required for location feature -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<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" />
<!--极光推送适配厂商通道添加权限 -->
<!-- 华为角标 -->
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
<!--honor 角标-->
<uses-permission android:name="com.hihonor.android.launcher.permission.CHANGE_BADGE"/>
<!-- VIVO角标权限 -->
<uses-permission android:name="com.vivo.notification.permission.BADGE_ICON" />
<uses-permission android:name="android.permission.VIBRATE" />
<!--振动器权限,JPUSH支持通知开启振动功能,小米推送必须-->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">Application"></span>
<activity
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">MainActivity"</span>
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">TestActivity"</span>
android:label="@string/app_name">
</activity>
<!-- 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 -->
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true" >
<intent-filter android:priority="1000"> <!--since 1.3.5 -->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--since 1.3.5 -->
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /> <!--since 1.3.5 --></span>
</intent-filter> <!--since 1.3.5 -->
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<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>
<receiver
android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your </span><span style="font-family: Arial, Helvetica, sans-serif;">Receiver"</span>
android:enabled="true" >
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>
</intent-filter>
</receiver>
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="<span style="font-family: Arial, Helvetica, sans-serif;">Your Package</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>
</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" />
<!-- 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="<span style="font-family: Arial, Helvetica, sans-serif;">Your <span style="color: rgb(50, 63, 72); font-family: ff-tisa-web-pro-1, ff-tisa-web-pro-2, 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'Microsoft YaHei UI', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 14px; line-height: 20px; background-color: rgb(235, 239, 243);">AppKey</span></span><span style="font-family: Arial, Helvetica, sans-serif;">"/> </span>
<!--极光推送适配华为通道添加service -->
<!--前提是lib下导入jpush-android-plugin-huawei-v4.8.5.jar -->
<service android:name="cn.jpush.android.service.PluginHuaweiPlatformsService"/>
</application>
</manifest>
3、接受广播
package com.itheima39.push;
import org.json.JSONObject;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Bundle bundle = intent.getExtras();
// Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Log.d(TAG, "JPush用户注册成功");
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的自定义消息");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的通知");
receivingNotification(context,bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");
openNotification(context,bundle);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void receivingNotification(Context context, Bundle bundle){
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
Log.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
Log.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.d(TAG, "extras : " + extras);
}
private void openNotification(Context context, Bundle bundle){
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String myValue = "";
try {
JSONObject extrasJson = new JSONObject(extras);
myValue = extrasJson.optString("url");
} catch (Exception e) {
Log.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
if (TYPE_THIS.equals(myValue)) {
Intent mIntent = new Intent(context, ThisActivity.class);
mIntent.putExtra("url", myValue);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
else if (TYPE_ANOTHER.equals(myValue)){
Intent mIntent = new Intent(context, AnotherActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
}
4、Your Application
package Your Package;
import cn.jpush.android.api.JPushInterface;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}
5、Your MainActivity 就是你的布局(随便!根据自己意愿创建布局,可以不用写!)
6、Your TestActivity 你想要根据推送跳转到的界面(如果指定的url地址,如果网络连接可以跳转到指定网址)
package Your Package;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class ThisActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thisactivity);
<span style="color:#33cc00;">//得到你指定的字段(设置的什么,它就会跳转到响应的地方!)</span>
String url = getIntent().getStringExtra("url");
Toast.makeText(getApplicationContext(), url, 0).show();
}
}
适配华为厂商通道
build.gradle文件中添加
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:+'
//testCompile 'junit:junit:4.12'
implementation 'com.jungly:gridPasswordView:0.3'
//***********需要在lib下导入以下jar包*****************
//JPush Android SDK 4.6.0 以上版本更新华为 HMS SDK 版本为:6.3.0.304 及以上版本,
// 接入要求:Android Studio 3.6.1 或更高版本、Android Gradle 插件 3.5.4 或更高版本。
implementation 'com.huawei.hms:push:6.3.0.304'
//5.3.0.301 4.0.0.300
implementation files('libs/jcore-android-4.0.0.jar')
implementation files('libs/jpush-android-4.8.5.jar')
implementation files('libs/jpush-android-plugin-huawei-v4.8.5.jar')
}
apply plugin: 'com.huawei.agconnect'
其他配置可以查看极光官网中厂商通道适配详情:极光文档
为什么需要接入厂商通道?
极光推送通过极光通道和厂商通道进行消息下发:1.极光通道是自建通道,需要依赖长连接才能收到推送,设备离线消息不会下发。2.厂商通道是系统通道,设备离线也可以收到推送。
说明:设备在线:指推送进程与极光服务器有长连接,一般指应用进程在线