应用场景
1.app在前台状态时,在app内推送
2.app在后台状态时,在消息栏推送
集成Jpush
极光推送集成Android 看看文档,先把sdk集成到项目内
逻辑分析
- 我们需要自定义Receiver 去接受jpush发送过来的消息
- 在接受到的消息中判断app在前台状态还是后台状态
- 根据前后台状态做出对应的业务需求
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
//判断当前app的状态是(前台,activity可见为前台)还是(后台,activity不可见为后台)
if (AppUtils.isAppOnForeground(context) &&MainApplication.getInstance().getForeground()) {
//前台状态
JPushInterface.clearAllNotifications(context);
MyLogUtils.INSTANCE.d(TAG,"在前台。。。。");
if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
Activity activity = ApplicationUtils.currentActivity();
shouDialog(activity)
} else {
//后台状态
MyLogUtils.INSTANCE.d(TAG,"在后台。。。。");
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else {
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] 用户点击打开了通知");
//打开自定义的Activity
Intent i = new Intent(context, TaskDetailActivity.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_RICHPUSH_CALLBACK.equals(intent.getAction())) {
MyLogUtils.INSTANCE.d(TAG, "[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);
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
MyLogUtils.INSTANCE.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
}
} catch (Exception e) {
}
设置消息栏的属性
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; //设置为点击后自动消失
builder.notificationDefaults = Notification.DEFAULT_SOUND; //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)
JPushInterface.setPushNotificationBuilder(100, builder);//100 这个属性看文档
这里有个细节,当你设置了这段代码,你有可能发现消息栏上的logo没有改变,这个时候你重启下手机就好了
结语
记一次需求,方便以后万一开发相同的需求用到,也说不定哪个小伙伴有类似的需求可以参考一下呢,好记性不如烂笔头,下次见咯。
网友评论