极光推送渠道号配置小坑

  就现有的第三方跨平台推送来说,极光推送算是对RN比较友好的一个,维护有官方RN插件,文档也算完善,基本上iOS/Andriod的简单接入看着文档能在1小时内搞定,不过在官方接入文档里会有个小坑,更准确说是manifestPlaceholders的坑,导致Android在使用纯数字渠道号(如:360)的情况下无法完成设备id的注册,也就无法收到后续的推送消息。

  其中渠道号根据极光的文档建议是在build.gradle中进行配置:

1
2
3
4
5
6
7
8
9
10
android {
defaultConfig {
applicationId "yourApplicationId"
...
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey
APP_CHANNEL: "developer-default" //应用渠道号
]
}
}

  然后在AndroidManifest.xml文件中再进行引用

1
2
3
4
5
6
7
<application
...
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}"/>
<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}"/>
</application>

  这在大多数情况下都是奏效的,但坑就坑在manifestPlaceholders的设置是弱类型的,解析时会根据值内容推断其类型,于是查看logcat(使用jpush关键字进行过滤)就会得到如下异常报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
12-24 12:18:21.107 26455-26455/? W/Bundle: Key JPUSH_CHANNEL expected String but value was a java.lang.Integer. The default value <null> was returned.
12-24 12:18:21.107 26455-26455/? W/Bundle: Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at android.os.BaseBundle.getString(BaseBundle.java:927)
at cn.jiguang.d.a.b(Unknown Source)
at cn.jiguang.d.a.a(Unknown Source)
at cn.jiguang.api.JCoreInterface.init(Unknown Source)
at cn.jpush.android.service.PushReceiver.onReceive(Unknown Source)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2758)
at android.app.ActivityThread.access$1900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

  解决起来倒是也简单,使用resValue指定变量类型为string即可:

1
2
3
4
5
6
7
8
9
10
android {
defaultConfig {
applicationId "yourApplicationId"
...
resValue "string", "APP_CHANNEL", "developer-default"
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey" //在此替换你的APPKey
]
}
}

  然后修改AndroidManifest.xml中的引用方式:

1
2
3
4
5
6
7
<application
...
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="@string/APP_CHANNEL"/>
<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}"/>
</application>

  再次打包安装,就可以发现日志中不再有jpush的报错,从控制台再发送一条消息也能收到了。