Android集成OPPO推送
2020-01-16 本文已影响0人
超人TIGA
官方文档地址:https://open.oppomobile.com/wiki/doc#id=10196
前置步骤:
1、使用OPPO企业开发者帐号,登陆OPPO开放平台,在“管理中心-应用服务平台-移动应用列表-选择应用-开发服务-推送服务”中完成OPPO PUSH权限申请
2、Opush申请通过后,可在OPPO推送平台-配置管理-应用配置-页面查看AppKey、AppSecret和MasterSecret。备注:仅开发者帐号(主帐号)可查看。
3、下载SDK:https://open.oppomobile.com/wiki/doc#id=10201
jar包名可以会不一样,新的好像是mcssdk-2.0.2.jar这个名字,我的是旧一些的版本,不过不影响,只要官网下载的都ok。
然后开始接入推送:
①在application中注册并初始化
private static final String OPPO_APP_KEY = "";
private static final String OPPO_APP_SECRET = "";
/**
* 初始化OPPO推送
*/
public static void initOppoPush(Context context) {
if (android.os.Build.BRAND.toLowerCase().contains("oppo")) {
//在执行Oppo推送注册之前,需要先判断当前平台是否支持Oppo推送
if (PushManager.isSupportPush(context)) {
PushManager.getInstance().register(context, OPPO_APP_KEY, OPPO_APP_SECRET, new PushAdapter() {
@Override
public void onRegister(int i, String s) {
if (i == ErrorCode.SUCCESS) {
//注册成功
Log.e("NPL", "注册成功,registerId=" + s);
} else {
//注册失败
Log.e("NPL", "注册失败");
}
}
});
}
}
}
②编写一个空白activity,用于接收和处理消息。
public class OPPOPushMessageActivity extends AppCompatActivity {
private int nativeRedirect = 0;
private String url = "";
private int pageType = 0;
private int newsType = 0;
private long newsId = 0;
private int openMethod = ActivityUtils.OPEN_IN;
private String thumb = "";
private String title = "";
private String content = "";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
// Log.d("OPPOPushMessageActivity","MainActivity is exist="+TnaotApplication.isExistMainActivity);
// UIUtils.showToast("MainActivity is exist="+TnaotApplication.isExistMainActivity);
FaceBookBehaviourUtil.sendBehaviour(this, FaceBookBehaviourUtil.ID_PUSH_MESSAGE);
if (intent.getExtras() != null) {
// 取参数值
Bundle bundle = intent.getExtras();
if (bundle.getString(MessageEntity.NativeRedirect) != null && !bundle.getString(MessageEntity.NativeRedirect).equals("")) {
nativeRedirect = Integer.valueOf(bundle.getString(MessageEntity.NativeRedirect));
}
url = bundle.getString(MessageEntity.Link);
if (bundle.getString(MessageEntity.PageType) != null && !bundle.getString(MessageEntity.PageType).equals("")) {
pageType = Integer.valueOf(bundle.getString(MessageEntity.PageType));
}
if (bundle.getString(MessageEntity.NewsType) != null && !bundle.getString(MessageEntity.NewsType).equals("")) {
newsType = Integer.valueOf(bundle.getString(MessageEntity.NewsType));
}
if (bundle.getString(MessageEntity.NewsId) != null && !bundle.getString(MessageEntity.NewsId).equals("")) {
newsId = Long.valueOf(bundle.getString(MessageEntity.NewsId));
}
if (bundle.getString(MessageEntity.OpenMethod) != null && !bundle.getString(MessageEntity.OpenMethod).equals("")) {
openMethod = Integer.valueOf(bundle.getString(MessageEntity.OpenMethod));
}
thumb = bundle.getString(MessageEntity.Thumb);
content = bundle.getString(MessageEntity.Description);
Intent startIntent = PushUtil.initIntentData(nativeRedirect, url, pageType, newsType, newsId, thumb, title, content);
startIntent.setClass(this, MainActivity.class);
startActivity(startIntent);
this.finish();
} else {
Intent startIntent = new Intent();
startIntent.setClass(this, WelcomeActivity2.class);
startActivity(startIntent);
this.finish();
}
}
}
③在AndroidManifest中配置该activity
<activity
android:name=".mctpush.oppoPush.OPPOPushMessageActivity"
android:configChanges="keyboardHidden|screenSize|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="com.cjy.test.action.oppo_push" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
其中的<action android:name="com.cjy.test.action.oppo_push" />,就是我们配置的action,官网或者后台api发送的时候,填上这个action,这个activity就会收到并处理这些消息(当用户点击该通知的时候)
④添加权限
<uses-permission android:name="com.coloros.mcs.permission.RECIEVE_MCS_MESSAGE" />
到这里已经基本配置完毕,已经能接受到消息推送的了。另外的详细API,如混淆、别名等设置,可以到官网直接查看,同时有些配置官方会持续更新,所以本文用作思路参考,具体最新配置方法还是最好看官网。