1、首先申请key
https://www.jiguang.cn/accounts/login/form
2、app build.gradle添加jar依赖
compile 'cn.jiguang.sdk:jpush:3.0.7'
compile 'cn.jiguang.sdk:jcore:1.1.3'
compile 'me.leolin:ShortcutBadger:1.1.16@aar'//消息桌面显示
注意最新依赖是:
compile 'cn.jiguang.sdk:jpush:3.1.6' // 此处以JPush 3.1.6 版本为例。 compile 'cn.jiguang.sdk:jcore:1.2.5' // 此处以JCore 1.2.5 版本为例。
在defaultConfig下添加key配置
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "880fe88703ab92260d7d42ca", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
如果项目加入了百度导航,需要在defaultConfig下添加系统配置
ndk {
abiFilters "armeabi", "x86", "x86_64", "mips64", "mips"
}
3、主要receiver
/**
* ProjectName: Myditukaifa
* Author: lgq
* Date: 2018/1/30 0030 14:37
*/
public class MyJPushReceiver extends BroadcastReceiver {
private String body;
private int num;
// private Intent mForegroundService;
private int tzid=0;
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String action = intent.getAction();
Log.i("lgqq","body=====自定义消息="+action);
if (action.equals("cn.jpush.android.intent.NOTIFICATION_OPENED")) {
//处理点击事件
ShareUtil.sharedPint("num",0);
ShortcutBadger.removeCount(context); //for 1.1.4+ 桌面信息
context.startActivity(new Intent(context, ShowJpushActivity.class).putExtra("tz",bundle.getString("cn.jpush.android.ALERT")));
}
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {//注册
// Util.soutLong(TAG, "JPush用户注册成功");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
// 这里会显示极光推送默认的通知,自定义能力有限
int num = ShareUtil.getSharedInt("num");
num++;
ShareUtil.sharedPint("num",num);
ShortcutBadger.applyCount(context, num);
Log.i("lgqq","body=====通知="+bundle.getString("cn.jpush.android.ALERT"));
}
String param = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.v("lgqq","body=====action="+num+".........."+param);
if (action.equals("cn.jpush.android.intent.NOTIFICATION_RECEIVED")) {
//处理滑动清除和点击删除事件
Log.v("lgqq","body=====shoudaotz=");
}
body = bundle.getString("cn.jpush.android.ALERT");
Log.v("lgqq","body======"+body);
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Logger.d("[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
// EventBus.getDefault().post(new LoginEvent(regId));
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Logger.d("[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
Log.i("lgqq","body=====接受到推送下来的消息=666666666666自定义消息6");
tzid=tzid+1;
int num = ShareUtil.getSharedInt("num");
num++;
ShareUtil.sharedPint("num",num);
ShortcutBadger.applyCount(context, num);
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Logger.d("[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Logger.d("[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Logger.d("[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Logger.w("[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
Logger.d("[MyReceiver] Unhandled intent - " + intent.getAction());
}
} catch (Exception e){
}
// context.startActivity(new Intent(context, TwoAcitvity.class));
}
//send msg to MainActivity
public String getTime(){
long currentTime = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分");
Date date = new Date(currentTime);
System.out.println(formatter.format(date));
// long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
String str=String.valueOf(formatter.format(date));
return str;
}
private void processCustomMessage(Context context, Bundle bundle) {
RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.kongreveiver);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String time = getTime();
// String hhmm = timetodate(time);
customView.setTextViewText(R.id.timemy,time);
if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_TITLE))){
customView.setTextViewText(R.id.name,bundle.getString(JPushInterface.EXTRA_TITLE));
}
customView.setTextViewText(R.id.neirongte,bundle.getString(JPushInterface.EXTRA_MESSAGE));
Intent intentCancel = new Intent(context,NotificationBroadcastReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context,0,
intentCancel,PendingIntent.FLAG_ONE_SHOT);
Intent companyIntroduce = new Intent(context, ShowJpushActivity.class);
companyIntroduce.putExtra("tz",bundle.getString(JPushInterface.EXTRA_MESSAGE));
// companyIntroduce.putExtra("name", name);
int notifyId = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(context, notifyId, companyIntroduce, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder//设置通知栏标题
// .setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE)) //设置通知栏显示内容
.setContent(customView)
.setContentIntent(pendingIntent) //设置通知栏点击意图
.setDeleteIntent(pendingIntentCancel)//取消消息回调
// .setTicker(context.getPackageName() + "消息")//通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
//.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
// .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
// .setDefaults(Notification.DEFAULT_SOUND)
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.mipmap.mylog);
// mNotificationManager.notify(notifyId, notify);
Log.i("lgqq","body=====id============"+tzid+".....");
int num = ShareUtil.getSharedInt("num");
num++;
ShareUtil.sharedPint("num",num);
mNotificationManager.notify(num, mBuilder.build());
}
}
4、次要receiver
/**
* 自定义JPush message 接收器,包括操作tag/alias的结果返回(仅仅包含tag/alias新接口部分)
* */
public class MyJPushMessageReceiver extends JPushMessageReceiver {
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
// TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);
Log.i("lgqq","body=====1111111=");
super.onTagOperatorResult(context, jPushMessage);
}
@Override
public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){
// TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage);
Log.i("lgqq","body=====2222222=");
super.onCheckTagOperatorResult(context, jPushMessage);
}
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
Log.i("lgqq","body=====33333=");
// TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage);
super.onAliasOperatorResult(context, jPushMessage);
}
}
5、配置receiver
6、权限,
<uses-permission android:name="com.tianxin.maient.liteapp.permission.JPUSH_MESSAGE" />//自己的包名
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<receiver android:name="com.tianxin.jpushs.MyJPushReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<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="com.tianxin.mapclient.liteapp" />//自己的包名
</intent-filter>
</receiver>
<receiver android:name="com.tianxin.jpushs.MyJPushMessageReceiver">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
<category android:name="com.tianxin.mapclient.liteapp"></category>//自己的包名
</intent-filter>
</receiver>
7、实例化
JPushInterface.init(context);
8、设置别名
JPushInterface.setAlias(this, //上下文对象
JPushUtil.getDeviceId(MainActivity.this), //别名
new TagAliasCallback() {//回调接口,i=0表示成功,其它设置失败
@Override
public void gotResult(int i, String s, Set<String> set) {
Log.d("alias", "set alias result is" + i);
}
});
9、去极光发送通知,即可收到通知
在线解决bug:qq1085220040
3130

被折叠的 条评论
为什么被折叠?



