美文网首页
融云和极光-小米推送冲突处理

融云和极光-小米推送冲突处理

作者: 你的益达233 | 来源:发表于2020-07-25 15:19 被阅读0次

明确实现的效果:

1、在应用不运行情况,通知栏能收到通知,点击通知栏能打开应用即可
2、应用在后台运行情况下(按home),通知栏能收到通知,点击通知栏跳转到相应界面

解决方案思路

重写集成融云的Receiver,只在AndroidManifest注册重写的Receiver,不注册极光的,然后在每个方法上极光也去接收相应的参数

首先融云的集成小米厂商的就按照融云提供的文档集成

这时应该能不运行情况下,能收到融云IM信息。如果不行,试着把极光的东西先移除。
运行情况下,会走融云本地通知的,也不用处理,点击会跳转会话界面

极光的集成小米厂商,也按照文档集成。

注:1、不能JCenter集成方式,要用AndroidManifest.xml集成方式,2、PluginXiaomiPlatformsReceiver不要注册
假设极光点击通知栏跳转逻辑如下(必须配置)(只是示例,具体看你们需求)

class MyJPushMessageReceiver : JPushMessageReceiver() {
private val TAG = "MyJPushMessageReceiver"

var jpushBean: JPushBean? = null
//自定义消息接收回调
override fun onMessage(context: Context, customMessage: CustomMessage) {
    LogUtil.json("MyJPushMessageReceiver", customMessage.toString())
    processCustomMessage(context, customMessage)
}

//收到通知回调
override fun onNotifyMessageArrived(context: Context, message: NotificationMessage) {
    
}

//打开通知回调
override fun onNotifyMessageOpened(context: Context, message: NotificationMessage) {
    
    val obj = JSONObject(message.notificationExtras)
    val json = obj.getJSONObject("param")
    if (json != null) {
        val goon = Gson()
        val type = object : TypeToken<JPushBean>() {}.type
        jpushBean = goon.fromJson(json.toString(), type)
    }


  
    when (jpushBean?.JumpType) {
        
        "4" -> {//推广文章详情
            //不用加登录
            ARouter.getInstance().build(Constant.CommonWebViewActivity)
                    .withString("title", "")
                    .withString("url", jpushBean?.JumpUrl)
                    .withString("shareTitle", "")
                    .withString("shareUrl", "")
                    .withString("shareContent", "")
                    .navigation()
        }
       
    }

}

override fun onMultiActionClicked(context: Context, intent: Intent) {
    LogUtil.d(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮")
    val nActionExtra = intent.extras!!.getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA)

    //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
    if (nActionExtra == null) {
        LogUtil.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null")
        return
    }
    when (nActionExtra) {
        "my_extra1" -> LogUtil.d(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一")
        "my_extra2" -> LogUtil.d(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二")
        "my_extra3" -> LogUtil.d(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三")
        else -> LogUtil.d(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义")
    }
}

//send msg to MainActivity
private fun processCustomMessage(context: Context, customMessage: CustomMessage) {
    LogUtil.json("MyJPushMessageReceiver", customMessage.toString())

}


override fun onNotifyMessageDismiss(context: Context, message: NotificationMessage) {
    LogUtil.d(TAG, "[onNotifyMessageDismiss] " + message!!)
}

override fun onRegister(context: Context, registrationId: String) {
    LogUtil.d(TAG, "[onRegister] " + registrationId!!)
}

override fun onConnected(context: Context, isConnected: Boolean) {
    LogUtil.d(TAG, "[onConnected] $isConnected")
}

override fun onCommandResult(context: Context, cmdMessage: CmdMessage) {
    LogUtil.d(TAG, "[onCommandResult] " + cmdMessage!!)
}

//tag 增删查改的操作会在此方法中回调结果。
override fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
    TagAliasOperatorHelper.instance.onTagOperatorResult(context, jPushMessage)
    super.onTagOperatorResult(context, jPushMessage)
}

//查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果。
override fun onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
    TagAliasOperatorHelper.instance.onCheckTagOperatorResult(context, jPushMessage)
    super.onCheckTagOperatorResult(context, jPushMessage)
}

//alias 相关的操作会在此方法中回调结果。
override fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
    TagAliasOperatorHelper.instance.onAliasOperatorResult(context, jPushMessage)
    super.onAliasOperatorResult(context, jPushMessage)
}

}

关键类:

public class MiPushReceiver extends MiMessageReceiver {

final PluginXiaomiPlatformsReceiver receiver = new PluginXiaomiPlatformsReceiver();

@Override
public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
    super.onReceivePassThroughMessage(context, miPushMessage);
    receiver.onReceivePassThroughMessage(context, miPushMessage);
}

@Override
public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
    LogUtil.INSTANCE.i("onNotificationMessageClicked","onNotificationMessageClicked");
    try {
        JMiPushBean jPushBean = new Gson().fromJson(miPushMessage.getContent(), JMiPushBean.class);
        //如果n_title有值代表是极光的推送
        //为什么要做这样的处理,因为融云的推送,点击它已经做好了,点击会进入相应的会话
        //极光的运行在前台点击通知栏跳转到相应界面也已经做好了,不运行情况下,点极光推送,启动应用即可
        if (jPushBean.getM_content() == null || jPushBean.getM_content().getN_title() == null || jPushBean.getM_content().getN_title().isEmpty()) {
            LogUtil.INSTANCE.i("onNotificationMessageClicked","onNotificationMessageClicked4");
            super.onNotificationMessageClicked(context, miPushMessage);
        } else {
            if (ActivityUtils.INSTANCE.isActivityExist(MainActivity.class)) {
                LogUtil.INSTANCE.i("onNotificationMessageClicked", "onNotificationMessageClicked2");
                receiver.onNotificationMessageClicked(context, miPushMessage);
            } else {
                LogUtil.INSTANCE.i("onNotificationMessageClicked", "onNotificationMessageClicked3");
                ARouter.getInstance().build(Constant.StartPageActivity).navigation();
            }
        }

    } catch (Exception e) {
        LogUtil.INSTANCE.i("onNotificationMessageClicked","onNotificationMessageClicked1");
        super.onNotificationMessageClicked(context, miPushMessage);
    }


}

@Override
public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageArrived(context, miPushMessage);
        receiver.onNotificationMessageArrived(context, miPushMessage);

}

@Override
public void onCommandResult(Context context, MiPushCommandMessage miPushCommandMessage) {
    super.onCommandResult(context, miPushCommandMessage);
    receiver.onCommandResult(context, miPushCommandMessage);
}

@Override
public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
    super.onReceiveRegisterResult(context, miPushCommandMessage);
    receiver.onReceiveRegisterResult(context, miPushCommandMessage);
}
}

示例(要改为你项目具体的)

<receiver android:name=".im.push.MiPushReceiver" 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>

有问题可加群:142739277
参考文档(我是在这篇文档上找到思路):https://blog.csdn.net/c10WTiybQ1Ye3/article/details/105445162

相关文章

网友评论

      本文标题:融云和极光-小米推送冲突处理

      本文链接:https://www.haomeiwen.com/subject/msxmlktx.html