Ionic3 Cordova Android 6.x集成最新极光华为/小米厂商点击通知跳转实现
- 一、添加跳转处理页面OpenClickActivity
- 二、MainActivity获取传递参数
- 三、JPushPlugin.java提供Java调用Js接口
- 四、JPushPlugin.js提供接口供JPushPlugin.java调用
- 五、在web项目app.page.ts中添加监听供Java传递数据
- 六、推送消息,验证结果
一、添加跳转处理页面OpenClickActivity
- 在应用包名下创建一个OpenClickActivity类
public class OpenClickActivity extends Activity {private static final String TAG = "JIGUANG";public static final String THIRD_PUSH_DATA = "THIRD_PUSH_DATA";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView mTextView = new TextView(this);setContentView(mTextView);handleOpenClick();}/*** 处理点击事件,当前启动配置的Activity都是使用* Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK* 方式启动,只需要在onCreate中调用此方法进行处理*/private void handleOpenClick() {Log.d(TAG, "用户点击打开了通知");String data = null;//获取华为平台附带的jpush信息if (getIntent().getData() != null) {data = getIntent().getData().toString();}//获取fcm/oppo/小米/vivo 平台附带的jpush信息if (TextUtils.isEmpty(data) && getIntent().getExtras() != null) {data = getIntent().getExtras().getString("JMessageExtra");}//将获取到的参数传到MainActivityIntent intent = new Intent(this, MainActivity.class);intent.putExtra(THIRD_PUSH_DATA, data);Log.d(TAG, "handleOpenClick: "+ data);startActivity(intent);finish();}
}
- 在AndroidManifest.xml中注册该类
<activity android:exported="true" android:name=".OpenClickActivity"><intent-filter><action android:name="pub.kilo.thirdpush.open" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
二、MainActivity获取传递参数
public class MainActivity extends CordovaActivity {/*** 应用第一次启动时*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Bundle extras = getIntent().getExtras();if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {moveTaskToBack(true);}// Set by <content src="index.html" /> in config.xmlloadUrl(launchUrl);// 获取到OpenClickActivity传递过来的参数,并且传给Cordova Html页面jpushThirdPushPlugin(extras, 2200L);}/*** 应用在线时,再次获取intent时*/@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);setIntent(intent);jpushThirdPushPlugin(intent.getExtras(), 0L);}/*** 极光厂商通道推送* @param extras*/private void jpushThirdPushPlugin(final Bundle extras, final Long timer) {new Thread() {@Overridepublic void run() {super.run();try {Thread.sleep(timer);//休眠3秒,// 在onCreate时避免webview未加载完html,} catch (InterruptedException e) {e.printStackTrace();}//获取到OpenClickActivity传递过来的参数,并且传给Cordova Html页面if (extras == null) return;String data = extras.getString(OpenClickActivity.THIRD_PUSH_DATA);if (!TextUtils.isEmpty(data)) {JPushPlugin.onThirdPushOpened(data);}}}.start();}
}
三、JPushPlugin.java提供Java调用Js接口
public static void onThirdPushOpened(String data) {if (instance == null) {return;}String format = "window.plugins.jPushPlugin.openThirdPushNotificationInAndroidCallback(%s);";final String js = String.format(format, data);cordovaActivity.runOnUiThread(new Runnable() {@Overridepublic void run() {instance.webView.loadUrl("javascript:" + js);}});}
四、JPushPlugin.js提供接口供JPushPlugin.java调用
打开路径app/src/main/assets/www/plugins/jpush-phonegap-plugin.www下的JPushPlugin.js,添加如下代码:
JPushPlugin.prototype.openThirdPushNotification = {};JPushPlugin.prototype.openThirdPushNotificationInAndroidCallback = function(data) {data = JSON.stringify(data);this.openThirdPushNotification = JSON.parse(data);cordova.fireDocumentEvent("jpush.openThirdPushNotification", this.openThirdPushNotification);
};
建议直接修改插件中的js代码,那么不用每次打包前进行一次修改。
插件包js文件路径/plugins/jpush-phonegap-plugin/www/JPushPlugin.js
五、在web项目app.page.ts中添加监听供Java传递数据
export class AppPage {constructor(private platform: Platform,){platform.ready().then(() => {// 极光厂商商通道通知点击监听document.addEventListener("jpush.openThirdPushNotification",(event: any) => {if (this.platform.is('android')) {let extras = event['n_extras']// 下面写你的业务代码} },false);}}
}
六、推送消息,验证结果
推送接口:https://api.jpush.cn/v3/push
body:
{"platform": "android","audience": {"alias": "all"},"notification": { "android": {"title": "资源申请","alert": "IT设备申请-入职台式电脑申请!","extras": { // 自定义参数"nav": "82","params": "9WJIP3W2U8UFTF1MS031GJQFNR8KL5E4"},// 具体的包名"uri_activity": "xx.xx.xxxx.OpenClickActivity"}},"options": {"third_party_channel": {"huawei":{"importance":"NORMAL","distribution": "first_ospush" // 推送优先走厂商通道下发,无效走极光通道下发},"xiaomi":{"importance":"NORMAL","distribution": "first_ospush","channel_id": "high_system"} }}
}