您的位置:首页 > 移动开发 > Android开发

Android Study 之 极光推送使用心得以及记录

2018-01-25 23:42 330 查看
LZ-says:啧啧,最近风起云淡,快过年喽~~~又是一件纠结的事儿。。。

前言

最近项目遇到推送,与后台贱人于一合计,果断采用了极光推送。在使用过程中,有爽有不爽,特意再次记录下。

使用记录

一、集成方式(远程依赖)

通过远程依赖方式接入,只需要简单配置如下:

compile 'cn.jiguang.sdk:jpush:3.1.1'
compile 'cn.jiguang.sdk:jcore:1.1.9'


设置支持的SO库架构:

ndk {
// 设置支持的SO库架构
abiFilters "armeabi", "arm64-v8a"
}


接着在build中设置如下:

manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "JPush上注册的包名对应的appkey",
JPUSH_CHANNEL: "暂时填写默认值即可",
]


不得不说极光这点做的还不错,只要以上几行代码即可快速接入,默认的自动配置相应的例如主配置文件内容等。

设置之后,即可达到消息推出的效果 (当然,这里忽略了注册账号,配置Android包名等等步骤,都是很easy,自行查看吧)

推送的情况也很是easy,左侧Icon,标题以及内容。

二、如何指定某个用户进行推送?

官方提供我们设置Tag或者Alias,这里我们选择Alias方式进行设置。

由于LZ这里有一个可以唯一标识用户的家伙,所以这里直接将此值设置为Alias即可,关键代码如下:

private static final int MSG_SET_ALIAS = 1001;

private void setAlias() {
String alias = userName;
if (TextUtils.isEmpty(alias)) {
Toast.makeText(this, "异常", Toast.LENGTH_SHORT).show();
return;
}
// 调用 Handler 来异步设置别名
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));
}

private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
@Override
public void gotResult(int code, String alias, Set<String> tags) {
String logs;
switch (code) {
case 0:
logs = "Set tag and alias success";
// 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后,以后不必再次设置了。
break;
case 6002:
logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
// 延迟 60 秒来调用 Handler 设置别名
mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
break;
default:
logs = "Failed with errorCode = " + code;
}
}
};

private final Handler mHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_SET_ALIAS:
// 调用 JPush 接口来设置别名。
JPushInterface.setAliasAndTags(getApplicationContext(),
(String) msg.obj,
null,
mAliasCallback);
break;
default:
}
}
};


按理说,这样子确实挺好,But,总有恶心的需求,推送默认的文本 加密了,,,加密了!!!

你说怎么破???

三、自定义消息

设置BroadcastReceiver :

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import cn.jpush.android.api.JPushInterface;

/**
* author : HLQ
* e-mail : 925954424@qq.com
* time   : 2018/01/25
* desc   : 极光自定义接收器
* version: 1.0
*/
public class JPushReceiver extends BroadcastReceiver {

private static String TAG = "HLQ_Struggle";

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
if (!TextUtils.isEmpty(message)) {
try {
String result = new AESCryptUtil().decrypt(message);
NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// 实例化NotificationCompat.Builde并设置相关属性
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// 设置小图标
.setSmallIcon(R.drawable.icon_app_logo)
// 设置通知标题
.setContentTitle("核验反馈")
// 设置通知内容
.setContentText(result)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// 通过builder.build()方法生成Notification对象,并发送通知,id=1
notifyManager.notify((int) (Math.random()*100), builder.build());
} catch (Exception e) {
e.printStackTrace();
}
}

}

}


主配置文件中配置自定义的BroadcastReceiver :

<!-- User defined.  For test only  用户自定义的广播接收器-->
<receiver
android:name=".receiver.JPushReceiver"
android:enabled="true"
android:exported="false">
<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="项目包名" />
</intent-filter>
</receiver>


为什么要这么弄呢?

默认的推送消息,在你拦截到接收的消息时,通知栏以及显示出来了,So,很无奈;

自定义消息时,极光只是默认将消息传入SDK而且不会显示在通知栏,所以,我们只能通过接收到消息的方式后,手动设置提示通知栏。

文末尾

东西虽少,但是实际操作过程中,确实遇到了坑,从默认切换自定义消息,不过好歹在下班半小时后成功搞完~~~

在此做个记录,顺便简单分享下~~~

希望会有更多的小伙伴源源不断加入分享大军~~~

参考资源地址

极光官方Android集成文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  极光推送