前段时间,组长让我把极光通知的内容拿到,可是愁坏了,看极光的demo看了半天,没有看懂,就放弃了,只有自己想办法了。结果发现和demo差不多,当时立马就懵了,不多说,说多了都是泪。还是说说怎么拿到通知的内容吧。
这个其实很简单,你如果仔细看极光的文档,一定能发现 有这个JPushInterface.EXTRA_EXTRA, 这个接口就是获得极光通知内容的,在你自己定义的接收广播里面接收这个。然后在Mainactivity里面动态注册一个广播,在你定义的接收广播里面,进行发送广播,在MainActivity里面,写你动态注册的广播,这样就可以在MainActivity里面,接收到极光发送通知的内容。
MainActivity里面写的代码如下:
private myMessageReceiver myMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
public static final String ALERT = "alert";
private void initview() {
String packageName = getPackageName();
String deviceId = ExampleUtil.getDeviceId(getApplicationContext());
Toast.makeText(getApplicationContext(), deviceId, Toast.LENGTH_LONG).show();
myMessageReceiver = new myMessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
MainActivityTwo.this.registerReceiver(myMessageReceiver, filter);
}
public class myMessageReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getExtras().getString(ALERT);
if(name!= null){
Log.d("contents", "contents===="+name);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
}
}
在Receiver中代码是:
String contents = bundle.getString(JPushInterface.EXTRA_ALERT);
Intent intent2 = new Intent();
intent2.setAction(MainActivityTwo.MESSAGE_RECEIVED_ACTION);
intent2.putExtra(MainActivityTwo.ALERT, contents);
context.sendBroadcast(intent2);
这样就可以,很简单的。