Android友盟推送
当前版本号:v3.0.5
1.下载SDK解压并导入(import module,compile project(':PushSDK')),里面有demo,用demo的包名去官网添加一个应用,然后替换demo中的manifest文件里面的appkey和message_secret,就可以直接运行。
2.初始化SDK
-
public class App extends Application {
-
-
public PushAgent mPushAgent;
-
-
-
public void onCreate() {
-
super.onCreate();
-
mPushAgent = PushAgent.getInstance(this);
-
//注册推送服务,每次调用register方法都会回调该接口
-
mPushAgent.register(new IUmengRegisterCallback() {
-
-
-
public void onSuccess(String deviceToken) {
-
//注册成功会返回device token
-
Log.e("deviceToken-------",deviceToken);
-
}
-
-
-
public void onFailure(String s, String s1) {
-
-
}
-
});
-
-
mPushAgent.setPushIntentServiceClass(UmengPushIntentService.class);
-
-
}
-
}
这样就已经可以测试了。
3.打开指定页面
包名加activity名,com.umeng.demo.SecondActivity
4.自定义参数
自定义一个service去继承UmengMessageService
public class UmengPushIntentService extends UmengMessageService {
-
-
public void onMessage(Context context, Intent intent) {
-
Intent data = new Intent(intent);
-
data.setClass(context, TestActivity.class);
-
//需为Intent添加Flag:Intent.FLAG_ACTIVITY_NEW_TASK,否则无法启动Activity。
-
data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
context.startActivity(data);
-
-
}
-
-
}
在跳转的activity中去接收数据并解析
-
public class TestActivity extends AppCompatActivity {
-
-
public static final String TAG = "TestActivity";
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_test);
-
-
-
final String message = getIntent().getStringExtra(MESSAGE_BODY);
-
if (TextUtils.isEmpty(message))
-
return;
-
final UMessage msg;
-
try {
-
msg = new UMessage(new JSONObject(message));
-
UTrack.getInstance(this).trackMsgClick(msg);
-
Map<String, String> extra = msg.extra;
-
Log.d(TAG, "message=" + message); //消息体
-
Log.d(TAG, "custom=" + msg.custom); //自定义消息的内容
-
Log.d(TAG, "title=" + msg.title); //通知标题
-
Log.d(TAG, "text=" + msg.text); //通知内容
-
AlertDialog dialog;
-
if (null != extra && extra.containsKey("msgType"))
-
dialog = new AlertDialog.Builder(this)
-
.setTitle("if消息提示")
-
.setMessage(msg.text+"11111")
-
.setCancelable(false)
-
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
-
-
public void onClick(DialogInterface dialog, int which) {
-
}
-
})
-
.create();
-
else
-
dialog = new AlertDialog.Builder(this)
-
.setTitle("else消息提示")
-
.setMessage(msg.text)
-
.setCancelable(false)
-
.setPositiveButton("确认", null)
-
.create();
-
-
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
-
-
public void onDismiss(DialogInterface dialog) {
-
onBackPressed();
-
}
-
});
-
dialog.show();
-
} catch (JSONException e) {
-
e.printStackTrace();
-
}
-
}
-
}
自定义的service需要在application类中调用
mPushAgent.setPushIntentServiceClass(UmengPushIntentService.class);
同时需要在manifest在注册
-
<service android:name=".UmengPushIntentService"
-
android:process=":push"/>