<!--云信key-->
<meta-data
android:name="com.netease.nim.appKey"
android:value="" />
<!-- 小米推送 -->
<!-- 配置的service和receiver -->
<meta-data
android:name="com.xiaomi.push.api_key"
android:value="" />
<meta-data
android:name="com.xiaomi.push.app_id"
android:value="" />
<meta-data
android:name="com.xiaomi.push.app_secret"
android:value=""
/>
### 自定义小米推送接收receiver
<receiver android:name=".XiaoMiMessageReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.ERROR" />
</intent-filter>
</receiver>
<service
android:name="com.xiaomi.push.service.XMPushService"
android:enabled="true"
android:process=":pushservice" />
<!--注:此service必须在3.0.1版本以后(包括3.0.1版本)加入-->
<service
android:name="com.xiaomi.push.service.XMJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":pushservice" />
<receiver
android:name="com.xiaomi.push.service.receivers.PingReceiver"
android:exported="false"
android:process=":pushservice">
<intent-filter>
<action android:name="com.xiaomi.push.PING_TIMER" />
</intent-filter>
</receiver>
<!-- 注:此service必须在3.0.1版本以后(包括3.0.1版本)加入 -->
<service
android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
android:enabled="true"
android:exported="true" />
<service
android:name="com.xiaomi.mipush.sdk.MessageHandleService"
android:enabled="true" /> <!-- 注:此service必须在2.2.5版本以后(包括2.2.5版本)加入 -->
<receiver
android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver
android:name="com.netease.nimlib.mixpush.mi.MiPushReceiver"
android:exported="true">
<intent-filter android:priority="0x7fffffff">
<action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
<action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
<action android:name="com.xiaomi.mipush.ERROR" />
</intent-filter>
</receiver>
<!-- vivo推送 -->
<!-- 配置的service, activity, receiver -->
<meta-data
android:name="com.vivo.push.api_key"
android:value="" />
<meta-data
android:name="com.vivo.push.app_id"
android:value="" />
<service
android:name="com.vivo.push.sdk.service.CommandClientService"
android:exported="true" />
<activity
android:name="com.vivo.push.sdk.LinkProxyClientActivity"
android:exported="false"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<receiver android:name="com.netease.nimlib.mixpush.vivo.VivoPushReceiver">
<intent-filter>
<!-- 接收 push 消息 -->
<action android:name="com.vivo.pushclient.action.RECEIVE" />
</intent-filter>
</receiver>
<receiver android:name=".nim.vivo.VivoPushMessageReceiver">
<intent-filter>
<!-- 接收push消息 -->
<action android:name="com.vivo.pushclient.action.RECEIVE" />
</intent-filter>
</receiver>
<!-- 华为推送 -->
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="" />
华为推送接收消息 跳转
<activity android:name="HmsPushActivity"
android:configChanges="keyboardHidden|screenSize|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 下面内容由您自定义 -->
<data
android:host="com.huawei.codelabpush"
android:path="/deeplink"
android:scheme="pushshmscheme" />
</intent-filter>
</activity>
<service
android:name=".nim.hms.HWNotifyReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.netease.nimlib.mixpush.hw.HWPushService"
android:exported="false">
<intent-filter>
<!-- 必须,用于接收token -->
<action android:name="com.huawei.android.push.intent.REGISTRATION"/>
<!-- 必须, 用于接收透传消息 -->
<action android:name="com.huawei.android.push.intent.RECEIVE"/>
<!-- 必须, 用于接收通知栏消息点击事件 此事件不需要开发者处理,只需注册就可以-->
<action android:name="com.huawei.intent.action.PUSH_DELAY_NOTIFY"/>
<action android:name="com.huawei.push.action.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- Oppo推送配置项 需要配置以下两项 -->
<meta-data
android:name="com.oppo.push.app_key"
android:value=""
/>
<meta-data
android:name="com.oppo.push.app_secret"
android:value=""
/>
oppo推送消息接受者
<service android:name=".OPPOPushMessageService"
android:permission="com.coloros.mcs.permission.SEND_MCS_MESSAGE">
<intent-filter>
<action android:name="com.coloros.mcs.action.RECEIVE_MCS_MESSAGE" />
</intent-filter>
</service>
oppo推送消息接受者
<service android:name=".PushMessageService"
android:permission="com.heytap.mcs.permission.SEND_PUSH_MESSAGE">
<intent-filter>
<action android:name="com.heytap.mcs.action.RECEIVE_MCS_MESSAGE"/>
<action android:name="com.heytap.msp.push.RECEIVE_MCS_MESSAGE" />
</intent-filter>
</service>
oppo推送消息接受者跳转
<activity android:name=".OPPOPushMessageActivity"
android:configChanges="keyboardHidden|screenSize|orientation"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name="com.netease.nimlib.mixpush.oppo.OppoPushService"
android:permission="com.coloros.mcs.permission.SEND_MCS_MESSAGE">
<intent-filter>
<action android:name="com.coloros.mcs.action.RECEIVE_MCS_MESSAGE" />
</intent-filter>
</service> <!-- 兼容Q以下版本 -->
<service
android:name="com.netease.nimlib.mixpush.oppo.OppoAppPushService"
android:permission="com.heytap.mcs.permission.SEND_PUSH_MESSAGE">
<intent-filter>
<action android:name="com.heytap.mcs.action.RECEIVE_MCS_MESSAGE" />
<action android:name="com.heytap.msp.push.RECEIVE_MCS_MESSAGE" />
</intent-filter>
</service>
注册
if (NIMUtil.isMainProcess(context)) {
if (isMIUI()) {
MiPushClient.registerPush(context, "", "");
} else if (isEMUI()) {
// 华为推送在此处添加以下代码
com.huawei.hms.support.common.ActivityMgr.INST.init(context)
//华为设置显示通知栏消息
HmsMessaging.getInstance(context).turnOnPush().addOnCompleteListener { task ->
// 获取结果
if (task.isSuccessful) {
LogUtils.e("turnOnPush successfully.")
} else {
LogUtils.e("turnOnPush failed.")
}
}
} else if (isOPPO()) {
// OPPO推送在此处添加以下代码
HeytapPushManager.init(context, true)
if (HeytapPushManager.isSupportPush()) {
/**
* OPPO_APP_KEY, OPPO_APP_SECRET
*/
HeytapPushManager.register(context, "", "", object : ICallBackResultService {
/**
* 注册的结果,如果注册成功,registerID就是客户端的唯一身份标识
* @param code
* @param registerID
*/
override fun onRegister(code: Int, registerID: String?) {
if (code == 0) {
LogUtils.e("===========================$registerID")
}else{
HeytapPushManager.getRegister()
}
}
override fun onUnRegister(responseCode: Int) {
}
//获取设置推送时间的执行结果
override fun onSetPushTime(responseCode: Int, pushTime: String?) {
}
//获取当前的push状态返回,根据返回码判断当前的push状态,返回码具体含义可以参考[错误码]
override fun onGetPushStatus(responseCode: Int, status: Int) {
if (responseCode == 0 && status == 0) {
LogUtils.e("Push状态正常=====code=$responseCode,status=$status")
} else {
LogUtils.e(("Push状态错误=========code=$responseCode,status=$status"))
}
}
//获取当前通知栏状态,返回码具体含义可以参考[错误码]
override fun onGetNotificationStatus(responseCode: Int, status: Int) {
if (responseCode == 0 && status == 0) {
LogUtils.e("通知状态正常========code=$responseCode,status=$status")
} else {
LogUtils.e("通知状态错误=========code=$responseCode,status=$status")
}
}
});
HeytapPushManager.requestNotificationPermission()
HeytapPushManager.openNotificationSettings()
HeytapPushManager.resumePush()
}
} else if (isVIVO()) {
//初始化vivo推送
PushClient.getInstance(context).initialize()
//并且打开推送服务
PushClient.getInstance(context).turnOnPush { i ->
if (i == 0) {
LogUtils.e("NPL打开推送服务成功")
} else {
LogUtils.e("NPL打开推送服务失败")
}
}
}
fun isMIUI(): Boolean {
val manufacturer: String = Build.MANUFACTURER
return "xiaomi".equals(manufacturer, ignoreCase = true)
}
fun isEMUI(): Boolean {
val manufacturer: String = Build.MANUFACTURER
return "HUAWEI".equals(manufacturer, ignoreCase = true)
}
fun isOPPO(): Boolean {
val manufacturer: String = Build.MANUFACTURER
return "OPPO".equals(manufacturer, ignoreCase = true) || "OnePlus".equals(manufacturer, ignoreCase = true) || "realme".equals(manufacturer, ignoreCase = true)
}
fun isVIVO(): Boolean {
val manufacturer: String = Build.MANUFACTURER
return "vivo".equals(manufacturer, ignoreCase = true)
}
class XiaoMiMessageReceiver : PushMessageReceiver() {
private var mRegId: String? = null
private var mTopic: String? = null
private var mAlias: String? = null
private var mAccount: String? = null
private var mStartTime: String? = null
private var mEndTime: String? = null
private var mMessage: String? = null
/**
* 用来接收服务器发送的透传消息
*
* @param context
* @param miPushMessage
*/
override fun onReceivePassThroughMessage(context: Context, miPushMessage: MiPushMessage) {
super.onReceivePassThroughMessage(context, miPushMessage)
mMessage = miPushMessage.content
if (!TextUtils.isEmpty(miPushMessage.topic)) {
mTopic = miPushMessage.topic
} else if (!TextUtils.isEmpty(miPushMessage.alias)) {
mAlias = miPushMessage.alias
} else if (!TextUtils.isEmpty(miPushMessage.userAccount)) {
mAccount = miPushMessage.userAccount
}
}
/**
* 用来接收服务器发来的通知栏消息(用户点击通知栏时触发)
*
* @param context
* @param miPushMessage
*/
override fun onNotificationMessageClicked(context: Context, miPushMessage: MiPushMessage) {
super.onNotificationMessageClicked(context, miPushMessage)
mMessage = miPushMessage.content
if (!TextUtils.isEmpty(miPushMessage.topic)) {
mTopic = miPushMessage.topic
} else if (!TextUtils.isEmpty(miPushMessage.alias)) {
mAlias = miPushMessage.alias
} else if (!TextUtils.isEmpty(miPushMessage.userAccount)) {
mAccount = miPushMessage.userAccount
}
val url = ""
// url = miPushMessage.getExtra().get(MessageEntity.Link);
val intentGo = Intent()
intentGo.putExtra("link", url)
intentGo.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
// intentGo.setClass(context, XiaoMiPushMessageActivity.class);
context.startActivity(intentGo)
}
/**
* 用来接收服务器发来的通知栏消息(消息到达客户端时触发,并且可以接收应用在前台时不弹出通知的通知消息)
*
* @param context
* @param miPushMessage
*/
override fun onNotificationMessageArrived(context: Context, miPushMessage: MiPushMessage) {
super.onNotificationMessageArrived(context, miPushMessage)
mMessage = miPushMessage.content
if (!TextUtils.isEmpty(miPushMessage.topic)) {
mTopic = miPushMessage.topic
} else if (!TextUtils.isEmpty(miPushMessage.alias)) {
mAlias = miPushMessage.alias
} else if (!TextUtils.isEmpty(miPushMessage.userAccount)) {
mAccount = miPushMessage.userAccount
}
}
/**
* 用来接受客户端向服务器发送注册命令消息后返回的响应。
*
* @param context
* @param miPushCommandMessage
*/
override fun onReceiveRegisterResult(context: Context, miPushCommandMessage: MiPushCommandMessage) {
super.onReceiveRegisterResult(context, miPushCommandMessage)
val command = miPushCommandMessage.command
val arguments = miPushCommandMessage.commandArguments
val cmdArg1 = if (arguments != null && arguments.size > 0) arguments[0] else null
val cmdArg2 = if (arguments != null && arguments.size > 1) arguments[1] else null
if (MiPushClient.COMMAND_REGISTER == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mRegId = cmdArg1
LogUtils.e("XiaoMiMessageReceiverxiaomi push id:$mRegId")
//在这里就可以保存mRegId了,传给后台服务器
}
}
}
/**
* 用来接收客户端向服务器发送命令消息后返回的响应
*
* @param context
* @param miPushCommandMessage
*/
override fun onCommandResult(context: Context, miPushCommandMessage: MiPushCommandMessage) {
super.onCommandResult(context, miPushCommandMessage)
val command = miPushCommandMessage.command
val arguments = miPushCommandMessage.commandArguments
val cmdArg1 = if (arguments != null && arguments.size > 0) arguments[0] else null
val cmdArg2 = if (arguments != null && arguments.size > 1) arguments[1] else null
LogUtils.e("=================小米推送====onCommandResult=======${miPushCommandMessage.resultCode}")
if (MiPushClient.COMMAND_REGISTER == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mRegId = cmdArg1
}
} else if (MiPushClient.COMMAND_SET_ALIAS == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mAlias = cmdArg1
}
} else if (MiPushClient.COMMAND_UNSET_ALIAS == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mAlias = cmdArg1
}
} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mTopic = cmdArg1
}
} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mTopic = cmdArg1
}
} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME == command) {
if (miPushCommandMessage.resultCode == ErrorCode.SUCCESS.toLong()) {
mStartTime = cmdArg1
mEndTime = cmdArg2
}
}
}
}
public class MixPushActivity extends Activity {
private static final String TAG = "MixPushActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
parseIntent();
}
void parseIntent() {
LogUtils.e("===============parseIntent==你好");
Intent intent = getIntent();
if (intent == null) {
return;
}
ArrayList<IMMessage> msgListExtra = (ArrayList<IMMessage>) intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
new DemoMixPushMessageHandler().onNotificationClicked(MixPushActivity.this,null);
}
@Override
protected void onStop() {
super.onStop();
finish();
}
}
//编写一个空白activity,用于接收和处理消息。
class OPPOPushMessageActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
val intent = intent
LogUtils.e("==============OPPOPushMessageActivity==onResume")
intent?.setClass(this, SplashActivity::class.java)
intent.putExtra(YunXinConstant.nimMessageChat, true)
intent.putExtra("homeType", 3)
startActivity(intent)
}
override fun onStop() {
super.onStop()
finish()
}
}
/**
* DataMessageCallbackService Android Q 以上
*/
class OPPOPushMessageService : DataMessageCallbackService() {
/**
* 透传消息处理,应用可以打开页面或者执行命令,如果应用不需要处理透传消息,则不需要重写此方法
*
* @param context
* @param message
*/
override fun processMessage(context: Context, message: DataMessage) {
super.processMessage(context, message)
val content = message.content
LogUtils.e("============OPPOPushMessageService=====content====$content")
// TestModeUtil.addLogString(PushMessageService.class.getSimpleName(), "Receive SptDataMessage:" + content);
// MessageDispatcher.dispatch(context, content);//统一处理
}
}
/**
* 如果应用需要解析和处理Push消息(如透传消息),则继承PushService来处理,并在Manifest文件中申明Service
* 如果不需要处理Push消息,则不需要继承PushService,直接在Manifest文件申明PushService即可
* CompatibleDataMessageCallbackService 兼容 Android Q 以下
*/
class PushMessageService : CompatibleDataMessageCallbackService() {
/**
* 透传消息处理,应用可以打开页面或者执行命令,如果应用不需要处理透传消息,则不需要重写此方法
*
* @param context
* @param dataMessage
*/
override fun processMessage(context: Context, dataMessage: DataMessage) {
super.processMessage(context.applicationContext, dataMessage)
val content = dataMessage.content
LogUtils.e("=====================PushMessageService=====processMessage====$content")
// TestModeUtil.addLogString(PushMessageService.class.getSimpleName(), "Receive SptDataMessage:" + content);
// MessageDispatcher.dispatch(context, content);//统一处理
}
}
public class VivoPushMessageReceiver extends OpenClientPushMessageReceiver {
private int nativeRedirect=0;
private String url="";
private int pageType=0;
private int newsType=0;
private long newsId=0;
private String thumb="";
private String title = "";
private String content = "";
@Override
public void onNotificationMessageClicked(Context context, UPSNotificationMessage upsNotificationMessage) {
title = upsNotificationMessage.getTitle();
content = upsNotificationMessage.getContent();
Map<String,String> map = upsNotificationMessage.getParams();
}
@Override
public void onReceiveRegId(Context context, String s) {
// SpUtil.saveNoClearString(context, SpKey.VIVO_PUSH_REGISTRATION_ID, s);
}
}
class HmsPushActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
val intent = intent
if (intent != null) {
// 方法1(参数之间用“&”相连)设置的数据通过如下方式获取
val sessionID = intent.getIntExtra("sessionID",-1)
val sessionType = intent.getStringExtra("sessionType")
intent.setClass(this, SplashActivity::class.java)
startActivity(intent)
}
}
override fun onStop() {
super.onStop()
finish()
}
}
class HWNotifyReceiver : HmsMessageService() {
//接收透传消息方法。
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
LogUtils.e("=============HWNotifyReceiver==onMessageReceived==$remoteMessage")
}
//发送上行消息成功回调方法。
override fun onMessageSent(s: String) {
super.onMessageSent(s)
}
//发送上行消息失败回调方法。
override fun onSendError(s: String, e: Exception) {
super.onSendError(s, e)
}
//服务端更新Token回调方法。
override fun onNewToken(s: String) {
super.onNewToken(s)
LogUtils.e("=============onNewToken=======$s")
}
//申请Token失败回调方法。
override fun onTokenError(e: Exception) {
super.onTokenError(e)
LogUtils.e("====================${e.toString()}")
}
//发送上行消息时如果使用了消息回执能力,消息到达应用服务器后,应用服务器的应答消息通过本方法回调给应用。
override fun onMessageDelivered(s: String, e: Exception) {
super.onMessageDelivered(s, e)
}
}
// 添加自定义推送文案以及选项,请开发者在各端(Android、IOS、PC、Web)消息发送时保持一致,以免出现通知不一致的情况
NimUIKit.setCustomPushContentProvider(DemoPushContentProvider())
/**
* 示例:
* 1.自定义的推送文案
* 2.自定义推送 payload 实现特定的点击通知栏跳转行为{@link }
* <p>
* 如果自定义文案和payload,请开发者在各端发送消息时保持一致。
*/
public class DemoPushContentProvider implements CustomPushContentProvider {
@Override
public String getPushContent(IMMessage message) {
return message.getContent();
}
@Override
public Map<String, Object> getPushPayload(IMMessage message) {
return getPayload(message);
}
@Override
public String getPushNickName(IMMessage message) {
return null;
}
@Override
public String getPushNickAvatar(IMMessage message) {
return null;
}
private Map<String, Object> getPayload(IMMessage message) {
if (message == null) {
return null;
}
HashMap<String, Object> payload = new HashMap<>();
int sessionType = message.getSessionType().getValue();
payload.put(YunXinConstant.yunXinSessionType, sessionType);
String sessionId = "";
if (message.getSessionType() == SessionTypeEnum.Team) {
sessionId = message.getSessionId();
} else if (message.getSessionType() == SessionTypeEnum.P2P) {
sessionId = message.getFromAccount();
}
if (!TextUtils.isEmpty(sessionId)) {
payload.put(YunXinConstant.yunXinSessionId, sessionId);
}
//华为推送
setHwField(payload, sessionType, sessionId);
//OPPO推送
setOppoField(payload, sessionType, sessionId);
return payload;
}
private void setOppoField(Map<String, Object> pushPayload, int sessionType, String sessionId) {
Map<String, Object> oppoField = new HashMap<>();
/**
* 点击动作类型:
* 0,启动应用;
* 1,打开应用内页(activity的intent action);
* 2,打开网页;
* 4,打开应用内页(activity);
* 【非必填,默认值为0】;
* 5,Intent scheme URL
*/
oppoField.put("click_action_type", 4);
oppoField.put("click_action_activity", "OPPOPushMessageActivity的全路径");
JSONObject obj = new JSONObject();
try {
obj.putOpt("sessionID", sessionId);
obj.putOpt("sessionType", sessionType);
} catch (JSONException e) {
e.printStackTrace();
}
oppoField.put("action_parameters", obj.toString());
pushPayload.put("oppoField", oppoField);
}
private void setHwField(Map<String, Object> pushPayload, int sessionType, String sessionId) {
Intent hwIntent = new Intent(Intent.ACTION_VIEW);
//pushshmscheme://com.huawei.codelabpush/deeplink 请看HmsPushActivity的action配置
// <data
// android:host="com.huawei.codelabpush"
// android:path="/deeplink"
// android:scheme="pushshmscheme" />
hwIntent.setData(Uri.parse("pushshmscheme://com.huawei.codelabpush/deeplink?"));
hwIntent.putExtra("sessionID", sessionId);
hwIntent.putExtra("sessionType", sessionType);
hwIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String intentUri = hwIntent.toUri(Intent.URI_INTENT_SCHEME);
//点击事件的内容
JSONObject clickAction = new JSONObject();
//通知的内容
JSONObject notification = new JSONObject();
try {
clickAction.putOpt("type", 1)
.putOpt("intent", intentUri);
notification.putOpt("click_action", clickAction);
pushPayload.put("hwField", notification);
} catch (JSONException e) {
e.printStackTrace();
}
}
}