配置 AndroidManifest.xml
极光官网文档中有相应的配置说明,这里就不详细写了。
关于自定义通知栏样式
JPush 通知推送到客户端时,默认使用手机的默认设置来显示通知栏,包括铃声、震动等效果。
如果开发者想要达到如下的效果,则需要使用“自定义通知栏样式”功能:
通知栏样式使用与默认不一样的设置,比如想要控制:
铃声、震动
显示图标
替换默认的通知栏样式。
推送消息时服务端和客户端都可进行对通知栏样式的修改,這主要讲解关于客户端怎样对通知栏进行修改。
客户端设置通知栏样式
自定义的通知栏样式,是在客户端进行设置的。
// 自定义Notification样式
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
getApplicationContext(),
R.layout.customer_notitfication_layout, R.id.icon, R.id.title,
R.id.text);
builder.layoutIconDrawable = R.drawable.ic_launcher;
builder.developerArg0 = "developerArg2";
JPushInterface.setPushNotificationBuilder(2, builder);
Toast.makeText(getApplicationContext(), "Custom Builder - 2",
Toast.LENGTH_SHORT).show();
消息的接收和界面跳转
我们有时候可能需要根据推送的不同消息来实现跳转不同的页面,这时候就可能需要用到附加字段了,我们在Broadcast Receiver来接受推送下来的消息,解析附加字段内容,来达到我们的目的。
package com.infzm.daily.know.receiver;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;
import com.infzm.daily.know.ArticleDetailActivity;
import com.infzm.daily.know.MainActivity;
import com.infzm.daily.know.utils.LogUtils;
public class PushReceiver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
LogUtils.logi(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: "+ printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
LogUtils.logi(TAG, "[PushReceiver] 接收Registeration Id : " + regId);
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
}else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 用户点击打开了通知");
String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
LogUtils.loge(TAG, "type:" + type);
try {
JSONObject jsonObject = new JSONObject(type);
String str = jsonObject.getString("key");
if (str.equals("1")) {
//打开自定义的Activity
Intent i = new Intent(context, MainActivity.class);
bundle.putInt("index", 1);
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 (JSONException e) {
e.printStackTrace();
}
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 用户收到到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);
LogUtils.logi(TAG, "[PushReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
LogUtils.logi(TAG, "[PushReceiver] Unhandled intent - " + intent.getAction());
}
}
// 打印所有的 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 {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
}