摘要:极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。平台提供整合了Android推送、iOS推送的统一推送服务。
废话不多说,先上客户端怎么集成极光推送的功能~
第一步:首先登录上帐号之后创建一个应用,记下里面应用标识(AppKey)、API MasterSecret(服务器端要用到这个)
第二步:SDK可去官网下载,按照这里面讲的一步一步来,这里很简单就不多说
第三步:上Demo~
(这里讲一个小小功能,开机启动服务)
Android手机在开机、重启的时候系统会发送一个广播(android.intent.action.BOOT_COMPLETED),我们所要做的就是自定义一个BroadcastReceiver,使用标签去过滤这个广播,然后在onReceive()方法里面,干你该干的事,(我这里去启动了一个服务,初始化极光推送服务,然后可以实现开机不启动应用也可以顺利接收到推送)
BootCompletedReceiver.java
package com.example.b;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootCompletedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d("yqb", "检测到开机启动,去启动服务");
Intent newIntent = new Intent(context, StartService.class);
context.startService(newIntent);
}
}
}
StartService.java
package com.example.b;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import cn.jpush.android.api.JPushInterface;
public class StartService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
JPushInterface.init(this); // 初始化 JPush
JPushInterface.setLatestNotifactionNumber(getApplicationContext(), 10);// 保留多少条通知数
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
注册广播和服务
<!-- 注册广播接收者 -->
<receiver android:name="com.example.b.BootCompletedReceiver" >
<intent-filter>
<!-- 通过标签过滤 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<!-- 注册Service -->
<service android:name="com.example.b.StartService" >
</service>
开始方法:
// 开机启动一个服务,去初始化极光推送
Intent intent = new Intent(MainActivity.this, StartService.class);
startService(intent);
Toast.makeText(MainActivity.this, "服务启动成功", Toast.LENGTH_LONG).show();
注意:记得在AndroidManifest.xml文件里面注册广播和服务;
package com.example.b;
import java.util.LinkedHashSet;
import java.util.Set;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
public class MainActivity extends Activity {
private final String TAG = "yqb";
private TextView mTextView;
private EditText mEditText;
private Button mButton;
public static boolean isForeground = false;
private static final int MSG_SET_TAGS = 1001;//设置tag
private static final int MSG_SET_ALIAS = 1002;//设置设备别名
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_SET_TAGS:
Log.d(TAG, "Set tags in handler.");
// 调用JPush API设置Tag
JPushInterface.setAliasAndTags(getApplicationContext(), null,
(Set) msg.obj, mTagsCallback);
break;
case MSG_SET_ALIAS:
Log.d(TAG, "Set alias in handler.");
// 调用JPush API设置alias
JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj,
null, mTagsCallback);
break;
default:
Log.i(TAG, "Unhandled msg - " + msg.what);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview1);
mEditText = (EditText) findViewById(R.id.editText1);
mButton = (Button) findViewById(R.id.button1);
// 与极光推送绑定tag
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag = mEditText.getText().toString().trim();
// 检查 tag 的有效性
if (TextUtils.isEmpty(tag)) {
Toast.makeText(MainActivity.this, "tag不能为空",
Toast.LENGTH_SHORT).show();
return;
}
// ","隔开的多个 转换成 Set
String[] sArray = tag.split(",");
SettagSet = new LinkedHashSet();
for (String sTagItme : sArray) {
if (!ExampleUtil.isValidTagAndAlias(sTagItme)) {
Toast.makeText(MainActivity.this, "格式不对",
Toast.LENGTH_SHORT).show();
return;
}
tagSet.add(sTagItme);
}
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_TAGS,
tagSet));
}
});
// 开机启动一个服务,去初始化极光推送
Intent intent = new Intent(MainActivity.this, StartService.class);
startService(intent);
Toast.makeText(MainActivity.this, "服务启动成功", Toast.LENGTH_LONG).show();
init();
registerMessageReceiver(); // used for receive msg
}
// 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。
private void init() {
JPushInterface.init(getApplicationContext());
JPushInterface.setLatestNotifactionNumber(MainActivity.this, 10);// 保留多少条通知数
}
@Override
protected void onResume() {
isForeground = true;
JPushInterface.onResume(this);
super.onResume();
}
@Override
protected void onPause() {
isForeground = false;
JPushInterface.onPause(this);
super.onPause();
}
@Override
protected void onDestroy() {
unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
// 这个广播是通过MyReceiver来接收到服务端发送给我们的自定义消息(不是通知)然后在通过广播的形式发送过来,之后更新UI
// 这个实现功能是客户端不去向服务器请求,直接服务器推送消息下来,然后直接在界面展示(或者你可以写自己逻辑)
private MessageReceiver mMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
// 接收服务器推送过来过来自定义消息,
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
if (!ExampleUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
}
setCostomMsg(showMsg.toString());
}
}
}
// 更新界面UI逻辑
private void setCostomMsg(String msg) {
if (null != mTextView) {
mTextView.setText(msg);
mTextView.setVisibility(android.view.View.VISIBLE);
}
}
// 通过TagAliasCallback的接口回调来判断tag是否与极光推送服务器绑定成功
private final TagAliasCallback mTagsCallback = new TagAliasCallback() {
@Override
public void gotResult(int code, String alias, Settags) {
String logs;
switch (code) {
case 0:// 绑定成功
logs = "Set tag and alias success";
Log.i(TAG, logs);
break;
case 6002:// 绑定失败,60s之后再次绑定
logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
Log.i(TAG, logs);
if (ExampleUtil.isConnected(getApplicationContext())) {
mHandler.sendMessageDelayed(
mHandler.obtainMessage(MSG_SET_TAGS, tags),
1000 * 60);
} else {
Log.i(TAG, "No network");
}
break;
default:
logs = "Failed with errorCode = " + code;
Log.e(TAG, logs);
}
// 用户绑定成功之后 toas通知
ExampleUtil.showToast(logs, getApplicationContext());
}
};
}
下面是自定义的BroadcastReceiver(用来处理所有由服务器发送下来的通知,消息)
package com.example.b;
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 android.util.Log;
import cn.jpush.android.api.JPushInterface;
/**
* 自定义接收器
*
* 如果不定义这个 Receiver,则: 1) 默认用户会打开主界面 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "运行自定义的通知");
Log.d(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.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
// send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
.getAction())) {
Log.d(TAG,
"[MyReceiver] 接收到推送下来的自定义消息: "
+ bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle
.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String content = bundle.getString(JPushInterface.EXTRA_ALERT);
Log.d(TAG, "Title 标题:: " + title + " " + "Content : 内容:" + content);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
JPushInterface.reportNotificationOpened(context,
bundle.getString(JPushInterface.EXTRA_MSG_ID));
// 打开自定义的Activity
Intent i = new Intent(context, TestActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent
.getAction())) {
Log.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);
Log.e(TAG, "[MyReceiver]" + intent.getAction()
+ " connected state change to " + connected);
} else {
Log.d(TAG, "[MyReceiver] 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();
}
// 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 (null != extraJson && extraJson.length() > 0) {
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {
}
}
context.sendBroadcast(msgIntent);
}
}
}
注释写的很详细,大家直接看代码吧;
主要就是appKey和masterSecret一定要与我们在极光推送后台建立应用的保持一致,然后初始化JPushClient,我已经在里面封装好了一些公共方法,大家直接调用就可以了,
下面是我写的test.java测试类
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// if (JPushClientExample.sendAllUserPush("测试标题", "大好人",
// JPushClientExample.NOTIFICATION)) {
// System.err.println("---->发送成功");
// }
if (JPushClientExample.sendTagUserPush("测试标题", "烂好人123", "456",
JPushClientExample.NOTIFICATION)) {
System.err.println("---->发送成功");
}
// msgId :1460604083 1791853663 1791857267
// String msg = JPushClientExample.getMsgStatistics("1460604083");
// System.out.println("统计发送的消息"+msg);
// String[] msgS = { "1460604083", "1791853663", "1791857267" };
// String msg = JPushClientExample.getMsgStatistics(msgS);
// System.out.println("统计发送的消息" + msg);
}
}
以上就是客户端的代码,没多少挺简单,主要就是那个自定义的广播接收者来根据不同的参数来判断是什么推送通知;
这个是我看的一篇博客上的,全是转载!http://www.devstore.cn/new/newInfo/948.html