设备:魅族 MX5
Android版本:5.1
SDK版本:
implementation 'cn.leancloud.android:avoscloud-sdk:4.7.9'
implementation('cn.leancloud.android:avoscloud-push:4.7.9@aar') { transitive = true }
相关代码:
private void push() {
AVPush push = new AVPush();
JSONObject object = new JSONObject();
try {
object.put("action", "com.ring.jk.UPDATE_STATUS");
object.put("name", "LeanCloud.");
} catch (JSONException e) {
e.printStackTrace();
}
push.setPushToAndroid(true);
push.setData(object);
push.setMessage("123");
push.sendInBackground(new SendCallback() {
@Override
public void done(AVException e) {
if (e == null) {
// push successfully.
} else {
// something wrong.
}
}
});
}
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"!!!!!!!!!!");
LogUtil.log.e(TAG, "Get Broadcat");
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.avos.avoscloud.Channel");
//获取消息内容
JSONObject json = new JSONObject(intent.getExtras().getString("com.avos.avoscloud.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
<receiver
android:name="com.ring.jk.MyCustomReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="com.ring.jk.UPDATE_STATUS" />
</intent-filter>
</receiver>