- BmobInstallation和普通对象一样,都有 objectId
- BmobInstallation.getInstallationId(this));方法获取的是本机的设备码(直接从本地获取)
- BmobInstallation.getCurrentInstallation(this).save();也是从本机获取(初始化),不管怎么样,这个对象都不联网(objectId 是null)
注意事项
1.使用推送 仍然需要先 initialize Bmob的基础服务
Bmob.initialize(this, "458ded1950dfa204d99193dac1d2ae1e");// 2.使用推送服务时的初始化操作BmobInstallation.getCurrentInstallation(this).save();
3.启动推送服务
BmobPush.startWork(this,"458ded1950dfa204d99193dac1d2ae1e");
删除 某个对象的某些字段 尽量使用Bmob提供的remove方法,不要想的太天真,人家就是这样设计的,不服也没办法(有些地方确实很坑)
坑爹地方
BmobInstallation installation2 = BmobInstallation.getCurrentInstallation(this);
installation2.unsubscribe("aaa");
installation2.save(); //正确执行
installation2.save(this, new SaveListener() { //错误的代码
@Override
public void onSuccess() {
App.showShortToast("保存成功");
}
@Override
public void onFailure(int i, String s) {
App.showShortToast("保存失败"+i+" "+s);
}
});
BmobInstallation 没有重写 save(Context context,listener)这个方法,导致执行时候 出错(两个方法执行完全不一样)
BmobInstallation save()方法的源码
public void save() {
final List var3 = this.channels;
final Context var2 = mContext;
if(var2 == null) {
Log.e(BmobInstallation.class.getName(), "9012-context is null.");
} else {
BmobQuery var4;
(var4 = new BmobQuery()).addWhereEqualTo("installationId", I.getInstallationId(var2));
var4.findObjects(var2, new FindListener() {
public final void onSuccess(List object) {
if(object.size() > 0) {
BmobInstallation object1 = (BmobInstallation)object.get(0);
if(BmobInstallation.subOrUnsub) {
object1.addAllUnique("channels", var3);
} else {
object1.removeAll("channels", var3);
}
object1.setDeviceType((String)null);
object1.setTimeZone((String)null);
object1.setInstallationId((String)null);
object1.update(var2, new UpdateListener() {
public final void onSuccess() {
}
public final void onFailure(int code, String msg) {
Log.e(BmobInstallation.class.getName(), "Update BmobInstallation Error: " + msg + "(" + code + ")");
}
});
} else {
BmobInstallation.Code(this.b, var2);
}
}
public final void onError(int code, String msg) {
Log.e(BmobInstallation.class.getName(), "Find BmobInstallation Error: " + msg + "(" + code + ")");
if(code == 101) {
BmobInstallation.Code(this.b, var2);
}
}
});
}
}
可以看出 save()方法也是先根据 installId查询BmobInstallation对象,然后执行的update操作
但是也明显可以看出 save方法没有作更新其他 参数的操作,
故更新我们 extend BmobInstallation扩展的参数就必须
也如同save()方法的操作,先 find获取 objectId,然后update(BmobObject的方法)即可
这点真的真的非常坑爹(很多时候只能查看源码去解决了 )
完整的一个使用 Demo (官方的)
package com.example.bmobexample.push;
public class ActBmobPush extends BaseActivity {
BmobPushManager bmobPush;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_push);
//开启debug服务后,可知晓push服务是否正常启动和运行
BmobPush.setDebugMode(true);
//
BmobPush.startWork(this, MainActivity.APPID);
bmobPush = new BmobPushManager(this);
mListview = (ListView) findViewById(R.id.listview);
mAdapter = new ArrayAdapter(this, R.layout.list_item,
R.id.tv_item, getResources().getStringArray(
R.array.bmob_push_list));
mListview.setAdapter(mAdapter);
mListview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
testBmob(position + 1);
}
});
BmobInstallation.getCurrentInstallation(this).save();
}
/**更新自定义的BmobInstallation字段
* @method updateBmobInstallation
* @return void
* @exception
*/
public void updateBmobInstallation(){
BmobQuery query = new BmobQuery();
query.addWhereEqualTo("installationId", BmobInstallation.getInstallationId(this));
query.findObjects(this, new FindListener() {
@Override
public void onSuccess(List object) {
// TODO Auto-generated method stub
if(object.size() > 0){
MyBmobInstallation mbi = object.get(0);
mbi.setUid("uid");
mbi.update(ActBmobPush.this,new UpdateListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
Log.i("bmob", "更新成功");
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
Log.i("bmob", "更新失败:"+msg);
}
});
}else{
}
}
@Override
public void onError(int code, String msg) {
// TODO Auto-generated method stub
}
});
}
private void testBmob(int pos) {
switch (pos) {
case 1:
BmobInstallation installation = BmobInstallation.getCurrentInstallation(this);
installation.subscribe("aaa");
installation.subscribe("bbb");
installation.save();
break;
case 2:
BmobInstallation installation2 = BmobInstallation.getCurrentInstallation(this);
installation2.unsubscribe("bbb");
installation2.save();
break;
case 3:
// 给所有终端推送
pushMessage("这是给所有终端推送的一条消息");
break;
case 4:
// 给某个Android终端推送
pushAndroidMessage("这是给指定Android终端推送的一条消息", "E54053E1D3A74C86B61809AA8D1AEF46");
break;
case 5:
// 给某个IOS终端推送
pushIOSMessage("这是给指定IOS终端推送的一条消息", "e2d4869619f61e0266561ce956e5d3cda153fef844242c6bf3f2c52d48fe98d4");
break;
case 6:
// 给某某渠道推送
pushChannelMessage("这是给指定渠道推送的一条消息", "aaa");
break;
case 7:
// 给不活跃用户推送消息
pushToInactive("给不活跃用户推送的消息");
break;
case 8:
pushToAndroid("给Android平台推送的消息");
break;
case 9:
pushToIOS("给IOS平台推送的消息");
break;
case 10:
pushToGeoPoint("根据地理信息位置推送的消息");
break;
}
}
/**
* 给所有人推送消息
*/
private void pushMessage(String message){
// bmobPush.pushMessage(message);
bmobPush.pushMessageAll(message);
}
/**
* 给指定Android用户推送消息
* @param message
* @param installId
*/
private void pushAndroidMessage(String message, String installId){
// bmobPush.pushMessage(message, installId);
BmobQuery query = BmobInstallation.getQuery();
query.addWhereEqualTo("installationId", installId);
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 给指定IOS用户推送
* @param message
* @param deviceToken
*/
private void pushIOSMessage(String message, String deviceToken){
BmobQuery query = BmobInstallation.getQuery();
query.addWhereEqualTo("deviceToken", deviceToken);
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 给指定渠道推送消息
* @param message
* @param channel
*/
private void pushChannelMessage(String message, String channel){
BmobQuery query = BmobInstallation.getQuery();
List channels = new ArrayList();
channels.add(channel);
query.addWhereContainedIn("channels", channels);
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 给不活跃用户推送消息
* @param message
*/
private void pushToInactive(String message){
BmobQuery query = BmobInstallation.getQuery();
query.addWhereLessThan("updatedAt", new BmobDate(new Date()));
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 给android平台终端推送
* @param message
*/
private void pushToAndroid(String message){
BmobQuery query = BmobInstallation.getQuery();
query.addWhereEqualTo("deviceType", "android");
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 给ios平台终端推送
* @param message
*/
private void pushToIOS(String message){
BmobQuery query = BmobInstallation.getQuery();
query.addWhereEqualTo("deviceType", "ios");
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
/**
* 根据地理信息位置做推送
* @param message
*/
private void pushToGeoPoint(String message){
BmobQuery query = BmobInstallation.getQuery();
query.addWhereWithinRadians("location", new BmobGeoPoint(112.934755, 24.52065), 1.0);
bmobPush.setQuery(query);
bmobPush.pushMessage(message);
}
}