80,351
社区成员
发帖
与我相关
我的任务
分享
package com.example.administrator.bmob;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.bmob.push.PushConstants;
public class Receiver extends BroadcastReceiver {
private NotificationManager manager;
private String STATIC_NORMAL_ACTION = "com.example.administrator.bmob.TEST";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
Log.d("bmob", "客户端收到推送内容:" + intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING));
String msg = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentText(msg)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setTicker("懒人账本提醒...")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
manager.notify(1, notification);
Intent data = new Intent(STATIC_NORMAL_ACTION);
Bundle bundle = new Bundle();
bundle.putString("data", msg);
data.putExtras(bundle);
context.sendOrderedBroadcast(data, null);
}
}
}
package com.example.administrator.bmob;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import cn.bmob.push.BmobPush;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String STATIC_NORMAL_ACTION = "com.example.administrator.bmob.TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
// 初始化BmobSDK
Bmob.initialize(this, "336be8699869f10b984f71c0c3cdb91a");
// 使用推送服务时的初始化操作
BmobInstallation.getCurrentInstallation(this).save();
// 启动推送服务
BmobPush.startWork(this, "336be8699869f10b984f71c0c3cdb91a");
/* IntentFilter filter = new IntentFilter(STATIC_NORMAL_ACTION);
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);*/
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(STATIC_NORMAL_ACTION)) {
Bundle bundle = getResultExtras(true);
String data = bundle.getString("data");
textView.setText(data);
}
}
}
}
Caused by: java.lang.InstantiationException: can't instantiate class com.example.administrator.bmob.MainActivity$MyReceiver; no empty constructor
package com.example.administrator.bmob;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.bmob.push.PushConstants;
public class Receiver extends BroadcastReceiver {
private NotificationManager manager;
private String STATIC_NORMAL_ACTION = "com.example.administrator.bmob.TEST";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
Log.e("bmob", "客户端收到推送内容:" + intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING));
String msg = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentText(msg)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setTicker("懒人账本提醒...")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
manager.notify(1, notification);
Intent data = new Intent(STATIC_NORMAL_ACTION);
Bundle bundle = new Bundle();
bundle.putString("data", msg);
data.putExtras(bundle);
context.sendOrderedBroadcast(data, null);
Log.e("data=",data.toString());
Log.e("tag", "3");
}
}
}
主界面代码:
package com.example.administrator.bmob;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.push.BmobPush;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private String STATIC_NORMAL_ACTION = "com.example.administrator.bmob.TEST";
private BroadcastReceiver receiver;
/**
* 动态注册时,broadcastReceiver最好注册在onStart()方法中,这样只要程序不被关闭,都能收到广播
* 最好不要注册在onCreate(Bundle savedInstanceState)方法里,因为在这个方法里注册后,立刻就失效了
*/
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(STATIC_NORMAL_ACTION);
registerReceiver(receiver, filter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
// 初始化BmobSDK
Bmob.initialize(this, "336be8699869f10b984f71c0c3cdb91a");
// 使用推送服务时的初始化操作
BmobInstallation.getCurrentInstallation(this).save();
// 启动推送服务
BmobPush.startWork(this, "336be8699869f10b984f71c0c3cdb91a");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(STATIC_NORMAL_ACTION)) {
Bundle bundle = intent.getExtras();
String data = bundle.getString("data");
Toast.makeText(context,data,Toast.LENGTH_SHORT).show();
textView.setText(data);
Log.e("tag", "1");
}
}
};
}
}