JPush 产品简介
Push 是经过考验的大规模 App 推送平台,每天推送消息量级为数百亿条。 开发者集成 SDK 后,可以通过调用 API 推送消息。同时,JPush 提供可视化的 web 端控制台发送通知,统计分析推送效果。 JPush 全面支持 Android, iOS, Winphone 三大手机平台。
消息形式
JPush 提供四种消息形式:通知,自定义消息,富媒体和本地通知。
创建应用
1. 极光官网注册账号
2. 登录后,点击服务中心
3. 点击立即进入,进入开发者平台
4. 创建应用
5. 创建以后,应用信息
6. 点击推送设置里面的去设置,设置应用包名
确定之后,就会可以下载Demo和APK文件了。
集成 SDK
jcenter 自动集成步骤
使用 jcenter 自动集成的开发者,不需要在项目中添加 jar 和 so,jcenter 会自动完成依赖;在 AndroidManifest.xml 中不需要添加任何 JPush SDK 相关的配置,jcenter 会自动导入。
注意 :如果需要处理收到的消息、使用 3.0.7 版本支持的别名与标签的新接口,AndroidManifest 中的自定义广播接收器仍需开发者手动配置,参考 SDK 压缩包里的 AndroidManifest.xml 样例文件。
1. android studio 的 Project 根目录的主 gradle 中配置了 jcenter 支持。(新建 project 默认配置就支持)
buildscript {
repositories {
jcenter()
}
......
}
allprojects {
repositories {
jcenter()
}
}
2. 在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量。
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush 上注册的包名.
......
ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的 Appkey ", //JPush 上注册的包名对应的 Appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
......
}
......
}
dependencies {
......
compile 'cn.jiguang.sdk:jpush:3.8.6' // 此处以JPush 3.8.6 版本为例。
compile 'cn.jiguang.sdk:jcore:2.5.5' // 此处以JCore 2.5.5 版本为例。
......
}
3. 增加极光的广播和服务类继承类
服务就是为了让极光服务更加的坚挺,进程不被杀死。
public class MyJCommonService extends JCommonService {
}
<!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
<!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
<service
android:name=".MyJCommonService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
广播是接受极光添加标签呀接口呀注册成功失败的监听,里面可以重写很多办法,来进行获取操作返回的信息。
public class MyJPushMessageReceiver extends JPushMessageReceiver {
}
<!-- Required since 3.0.7 -->
<!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定的广播 -->
<!-- 3.3.0开始所有事件将通过该类回调 -->
<!-- 该广播需要继承 JPush 提供的 JPushMessageReceiver 类, 并如下新增一个 Intent-Filter -->
<receiver
android:name=".MyJPushMessageReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<!-- 您应用的包名-->
<category android:name="com.zly.myjpushdemo" />
</intent-filter>
</receiver>
4. 在Application中初始化极光
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//需在 init 之前调用,避免出现部分日志没打印的情况。多进程情况下建议在自定义的 Application 中 onCreate 中调用。
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
JPushInterface.init(this); // 初始化 JPush
}
}
5.权限
<!-- Required -->
<permission
android:name="com.zly.myjpushdemo.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!-- Required 一些系统要求的权限,如访问网络等-->
<uses-permission android:name="${applicationId}.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.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.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--华为角标-->
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
<!-- Optional for location -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <!-- 用于开启 debug 版本的应用在6.0 系统上 层叠窗口权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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_BACKGROUND_LOCATION" /><!-- Android Q后台定位权限-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
至此,就实现了极光的android端集成。更新SDK版本号很是轻松,不像以前还需要导入本地依赖jar包进行配置,和权限什么的,有了jcenter,舒服很多。
最后运行程序,发送广播测试一波
一次点击输入内容,最后点击最下面的发送预览,这样的你的程序就接收到了推送的消息了。
下面的什么的标签(tag)、别名(aliias)什么的,就是对用户的指定发送,只需要在极光初始化后进行用户绑定就行,这样你就能对指定的用户进行绑定了,具体绑定方法见。
设置角标和点击通知跳转页面:
手动集成步骤
具体步骤看官方文档
建议使用jcenter 自动集成步骤。
极光通过 广播来处理接收的消息
收到极光的推送,想自己做处理,有两种方法。
第一种:自定义类继承JPushMessageReceiver,使用极光的广播重写需要的方法,这里我粘贴一段例子中的代码
/**
* Cerated by xiaoyehai
* Create date : 2020/11/6 13:35
* description : 处理收到消息的广播接收者,继承JPushMessageReceiver
*/
public class MyJPushMessageReceiver extends JPushMessageReceiver {
private static final String TAG = "MyJPushMessageReceiver";
/**
* 收到自定义消息回调
*
* @param context
* @param customMessage
*/
@Override
public void onMessage(Context context, CustomMessage customMessage) {
Log.e(TAG, "[onMessage] " + customMessage);
//处理自定义消息
// processCustomMessage(context, customMessage);
}
/**
* 收到通知回调
*
* @param context
* @param message
*/
@Override
public void onNotifyMessageArrived(Context context, NotificationMessage message) {
Log.e(TAG, "[onNotifyMessageArrived] " + message);
}
/**
* 点击通知回调跳转到app的某一个页面
*
* @param context
* @param message
*/
@Override
public void onNotifyMessageOpened(Context context, NotificationMessage message) {
Log.e(TAG, "[onNotifyMessageOpened] " + message);
try {
//打开自定义的Activity
Intent i = new Intent(context, PushIntentActivity.class);
Bundle bundle = new Bundle();
bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE, message.notificationTitle);
bundle.putString(JPushInterface.EXTRA_ALERT, message.notificationContent);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
} catch (Throwable throwable) {
}
}
/**
* 通知的MultiAction回调
*
* @param context
* @param intent
*/
@Override
public void onMultiActionClicked(Context context, Intent intent) {
Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);
//开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
if (nActionExtra == null) {
Log.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
return;
}
if (nActionExtra.equals("my_extra1")) {
Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
} else if (nActionExtra.equals("my_extra2")) {
Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
} else if (nActionExtra.equals("my_extra3")) {
Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
} else {
Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
}
}
/**
* 清除通知回调
*
* @param context
* @param message
*/
@Override
public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
Log.e(TAG, "[onNotifyMessageDismiss] " + message);
}
/**
* 注册成功回调
*
* @param context
* @param registrationId
*/
@Override
public void onRegister(Context context, String registrationId) {
Log.e(TAG, "[onRegister] " + registrationId);
}
/**
* 长连接状态回调
*
* @param context
* @param isConnected
*/
@Override
public void onConnected(Context context, boolean isConnected) {
Log.e(TAG, "[onConnected] " + isConnected);
}
/**
* 注册失败回调
*
* @param context
* @param cmdMessage
*/
@Override
public void onCommandResult(Context context, CmdMessage cmdMessage) {
Log.e(TAG, "[onCommandResult] " + cmdMessage);
}
/**
* tag 增删查改的操作会在此方法中回调结果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
super.onTagOperatorResult(context, jPushMessage);
}
/**
* 查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
super.onCheckTagOperatorResult(context, jPushMessage);
}
/**
* alias 相关的操作会在此方法中回调结果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
super.onAliasOperatorResult(context, jPushMessage);
}
/**
* 设置手机号码会在此方法中回调结果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
super.onMobileNumberOperatorResult(context, jPushMessage);
}
//send msg to MainActivity
private void processCustomMessage(Context context, CustomMessage customMessage) {
/* if (MainActivity.isForeground) {
String message = customMessage.message;
String extras = customMessage.extra;
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
}*/
}
@Override
public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
super.onNotificationSettingsCheck(context, isOn, source);
Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
}
}
第二种、自己写一个自定义的广播,然后去手动监听,完成处理,但是记住如果在极光自带的广播里调用了拦截的方法,一定要super,不然你的自定义广播接收不到,下面是我的代码
/**
* 自定义接收器
*
* 如果不定义这个 Receiver,则:
* 1) 默认用户会打开主界面
* 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
Log.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.e(TAG, "[onReceive] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.e(TAG, "[onReceive] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.e(TAG, "[onReceive] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.e(TAG, "[onReceive] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.e(TAG, "[onReceive] 用户点击打开了通知");
//打开自定义的Activity
Intent i = new Intent(context, PushIntentActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Logger.w(TAG, "[onReceive]" + intent.getAction() +" connected state change to "+connected);
} else {
Log.e(TAG, "[onReceive] Unhandled intent - " + intent.getAction());
}
} catch (Exception e){
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Logger.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " +json.optString(myKey) + "]");
}
} catch (JSONException e) {
Logger.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.get(key));
}
}
return sb.toString();
}
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) {
/*if (MainActivity.isForeground) {
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
}*/
}
}
<!-- User defined. For test only 用户自定义的广播接收器-->
<receiver
android:name=".jpush.MyReceiver"
android:enabled="true">
<intent-filter>
<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 用户接收SDK通知栏信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required 用户打开自定义通知栏的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 -->
<category android:name="com.zly.myjpushdemo" /> <!-- 您应用的包名-->
</intent-filter>
</receiver>
上面2中方式 使用其中一种方式就可以,建议使用第一种。
上面2中方式如果都写了:
如果MyJPushMessageReceiver 重写了对用的方法,则回调MyJPushMessageReceiver的方法,不走MyReceiver的回调;
如果MyJPushMessageReceiver没有 重写对用的方法,则回调MyReceiver配置了对应广播的receiver中。
比如:如果重写了 onNotifyMessageOpened 则回调到该方法,如果未重写 onNotifyMessageOpened ,则回调到配置了cn.jpush.android.intent.NOTIFICATION_OPENED 广播的receiver中。
PushIntentActivity
push中转页,主要负责push跳转到各个功能页面。
/**
* push中转页,主要负责push跳转到各个功能页面
*/
public class PushIntentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("用户自定义打开的Activity");
Intent intent = getIntent();
if (null != intent) {
Bundle bundle = getIntent().getExtras();
String title = null;
String content = null;
if(bundle!=null){
title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
content = bundle.getString(JPushInterface.EXTRA_ALERT);
}
tv.setText("Title : " + title + " " + "Content : " + content);
}
addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
接收推送消息 Receiver
JPush SDK 收到推送,通过广播的方式,转发给开发者 App,这样开发者就可以灵活地进行处理。
这个动作不是必须的。用户有需要才定义 Receiver 类来处理 SDK 过来的广播。
如果不做这个动作,即不写自定义 Receiver,也不在 AndroidManifest.xml 里配置这个 Receiver,则默认的行为是:
- 接收到推送的自定义消息,则没有被处理
- 可以正常收到通知,用户点击打开应用主界面
3.3.0开始使用新的消息回调方式
如果你依然需要在这个Receiver里接收到回调,则使用新的回调方式以后,不重写对应回调的方法,或者重写回调方法且调用super方法;
如果你不需要在这个 Receiver 接收,则使用新的回调方式,然后重写对应回调方法,不调用super方法。
接收广播
如果全部类型的广播都接收,则需要在 AndroidManifest.xml 里添加如下的配置信息:
<receiver
android:name="Your Receiver"
android:enabled="true"
android:exported="false">
<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" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="You package Name" />
</intent-filter>
</receiver>
开发者自定义 Receiver 代码示例
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "onReceive - " + intent.getAction());
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收 Registration Id : " + regId);
}else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
// 自定义消息不会展示在通知栏,完全要开发者写代码去处理
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "收到了通知");
// 在这里可以做些统计,或者做些其他工作
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");
// 在这里可以自己写代码去定义用户点击后的行为
Intent i = new Intent(context, TestActivity.class); //自定义打开的界面
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
更多示例代码请参考 Android SDK 压缩包中的 example 工程。
别名与标签
别名 alias
温馨提示,设置标签别名请注意处理 call back 结果,只有设置成功才可以向目标推送,否则服务器 API 会返回 1011 错误。
从 3.0.7 版本开始,别名和标签是异步回调,注意在 Androidmanifest 里面配置自定义广播接收器
为安装了应用程序的用户,取个别名来标识。以后给该用户 Push 消息时,就可以用此别名来指定。每个用户只能指定一个别名。
同一个应用程序内,对不同的用户,建议取不同的别名。这样,尽可能根据别名来唯一确定用户。
系统不限定一个别名只能指定一个用户。如果一个别名被指定到了多个用户,当给指定这个别名发消息时,服务器端 API 会同时给这多个用户发送消息。
举例:在一个用户要登录的游戏中,可能设置别名为 userid。游戏运营时,发现该用户 3 天没有玩游戏了,则根据 userid 调用服务器端 API 发通知到客户端提醒用户。
注意:极光于 2020/03/10 对「别名设置」的上限进行限制,最多允许绑定 10 个设备。如需更高上限,请联系商务,详情请阅读公告。
标签 tag
为安装了应用程序的用户,打上标签。其目的主要是方便开发者根据标签,来批量下发 Push 消息。
可为每个用户打多个标签。
举例: game, old_page, women
Method - filterValidTag
使用建议
设置 tags 时,如果其中一个 tag 无效,则整个设置过程失败。 如果 App 的 tags会在运行过程中动态设置,并且存在对 JPush SDK tag 规定的无效字符,则有可能一个 tag 无效导致这次调用里所有的 tags 更新失败。 这时你可以调用本方法 filterValidTags 来过滤掉无效的tags,得到有效的 tags, 再调用 JPush SDK 的 set tags / alias 方法。
public static Set<String> filterValidTags(Set<String> tags);
接口返回有效的 tag 集合。
新别名 alias 与标签 tag 接口
新别名与标签接口支持增删改查的功能,从 3.0.7 版本开始支持,老版本别名与标签的接口从 3.0.7 版本开始不再维护。
注意:极光于 2020/03/10 对「别名设置」的上限进行限制,最多允许绑定 10 个设备。如需更高上限,请联系商务,详情请阅读公告。
回调说明
新别名 alias 与标签 tag 接口回调触发 cn.jpush.android.service.JPushMessageReceiver,详细的回调方法请参考新的消息回调方式说明。
设置手机号码接口
public static void setMobileNumber(Context context,int sequence, String mobileNumber);
通知栏样式定制
大多数情况下,开发者不需要调用这里的定制通知栏 API 来自定义通知栏样式,只需要使用 SDK 默认的即可。
如果您想:
- 改变 Notification 里的铃声、震动、显示与消失行为
- 自定义通知栏显示样式
- 不同的 Push 通知,Notification样式不同
则请使用本通知栏定制 API 提供的能力。
//自定义通知栏样式(解决通知栏图标变形问题)
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(context);
builder.statusBarDrawable = R.mipmap.ic_launcher;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_SHOW_LIGHTS; //设置为自动消失和呼吸灯闪烁
builder.notificationDefaults = Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_LIGHTS; // 设置为铃声、震动、呼吸灯闪烁都要
JPushInterface.setPushNotificationBuilder(1, builder);
设置保留最近通知条数
通过极光推送,推送了很多通知到客户端时,如果用户不去处理,就会有很多保留在那里。
从 v 1.3.0 版本开始 SDK 增加此功能,限制保留的通知条数。默认为保留最近 5 条通知。
开发者可通过调用此 API 来定义为不同的数量。
仅对通知有效。所谓保留最近的,意思是,如果有新的通知到达,之前列表里最老的那条会被移除。
例如,设置为保留最近 5 条通知。假设已经有 5 条显示在通知栏,当第 6 条到达时,第 1 条将会被移除。
//context 应用的 ApplicationContext maxNum 最多显示的条数
public static void setLatestNotificationNumber(Context context, int maxNum)
本接口可以在 JPushInterface.init 之后任何地方调用。可以调用多次。SDK 使用最后调用的数值。
JPushInterface.init(context);
JPushInterface.setLatestNotificationNumber(context, 3);
角标
//context 是应用的 ApplicationContext num 新的角标数字,传入负数将会修正为0
public static void setBadgeNumber(Context context, int num)
设置角标数字(目前仅支持华为手机),如果需要调用这个接口,还需要在AndroidManifest.xml里配置华为指定的权限。
<!--华为角标-->
<uses-permission
android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
常用API
设置调试模式
该接口需在 init 接口之前调用,避免出现部分日志没打印的情况。多进程情况下建议在自定义的 Application 中 onCreate 中调用。
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
初始化推送服务 API
初始化推送服务,调用了本 API 后,JPush 推送服务进行初始化。建议在自定义的 Application 中的 onCreate 中调用。
JPushInterface.init(getApplicationContext());
停止推送服务
调用了本 API 后,JPush 推送服务完全被停止,极光推送所有的其他 API 调用都无效,不能通过 JPushInterface.init 恢复,需要调用 resumePush 恢复。
JPushInterface.stopPush(getApplicationContext());
恢复推送服务
调用了此 API 后,极光推送完全恢复正常工作。
JPushInterface.resumePush(getApplicationContext());
isPushStopped
用来检查 Push Service 是否已经被停止。
public static boolean isPushStopped(Context context);
获取RegistrationID
String rid = JPushInterface.getRegistrationID(getApplicationContext());
if (!rid.isEmpty()) {
mRegId.setText("RegId:" + rid);
} else {
Toast.makeText(this, "Get registration fail, JPush init failed!", Toast.LENGTH_SHORT).show();
}
registerToken
集成 JPush Android SDK 的混淆
请下载 4.x 及以上版本的 proguard.jar, 并替换你 Android SDK “tools\proguard\lib\proguard.jar”
请在工程的混淆文件中添加以下配置:
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep class * extends cn.jpush.android.helpers.JPushMessageReceiver { *; }
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }
极光推送集成厂商通道
极光推送集成厂商通道目的
保证应用被杀死进程后仍能收到推送的消息。
概述
在国内Android生态中,推送通道都是由终端与云端之间的链接来维持,严重依赖于应用进 程的存活状态。如今一些手机厂家会在自家Rom中做系统级别的推送通道,再由系统分发给 各个app,以此提高在自家Rom上的推送送达率。
JPushSDK为了尽可能提高开发者在各类Rom上的推送送达率,对使用小米、华为、OPPO、vivo、魅族以及海外支持Google服务的设备(以下简称为海外设备)推送,自动 切换到相应的通道。同时,为了保证SDK的易用性,原本JPush的所有接口调用逻辑都不用修 改,JPush会对自身支持的功能做兼容.只需配置上相应的SDK必须的配置组件即可。
小米、华为、魅族、FCM 、OPPO、vivo、这五种通道依次在JPushAndroidSDK的 3.0.3、3.0.5、3.0.6、3.1.0、3.1.5、3.2.0版本(下载最新版本)得到支持,请注意当 前JPushAndroidSDK的版本。
厂商通道相关参数申请后,要在极光后台配置
极光推送厂商通道配置文档3.8.5资料
链接:https://pan.baidu.com/s/1sUfVI05J6DoLJN4NH_u29A
提取码:det7