android 端极光推送集成,适用于webview

来源:互联网 发布:买美股用什么软件 编辑:程序博客网 时间:2024/05/16 01:22

1:激光的基本集成官方都有例子,我这里说重点。
在集成里面有一个class MyReceiver ,里面有用户点击打开通知。我在里面主要写得操作是:
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, “[MyReceiver] 用户点击打开了通知”);
//打开自定义的Activity
Intent i= new Intent(context,TaiHuLuMainActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
在这个方法里面有要跳转的主程序,在这个里面这个代码主要是看是否应用打开,直接置顶 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
还有这个class 有一个
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, “[MyReceiver] 接收Registration Id : ” + regId);
这个ID就是每个设备自动生成 的一个识别码,这个识别码,对于自己的后台的推送很有用,这个参数必须传到后台的,后台的那一部分,可以参考激光推动自定义后台的demo.
后面的重点就是推送过来的消息显示地方,在webview 显示的url 。
在MyReceiver里面有一个发送消息的
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) {
if (MainActivity.isForeground) {
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
if (!ExampleUtil.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (null != extraJson && extraJson.length() > 0) {
// URL= extraJson.optString(“url”);
msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
}
} catch (JSONException e) {

            }        }        context.sendBroadcast(msgIntent);    }}

而在主activity里面就是接收消息的代码:
// for receive customer msg from jpush server
private MessageReceiver mMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = “com.example.jpushdemo.MESSAGE_RECEIVED_ACTION”;
public static final String KEY_TITLE = “title”;
public static final String KEY_MESSAGE = “message”;
public static final String KEY_EXTRAS = “extras”;

        public void registerMessageReceiver() {            mMessageReceiver = new MessageReceiver();            IntentFilter filter = new IntentFilter();            filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);            filter.addAction(MESSAGE_RECEIVED_ACTION);            registerReceiver(mMessageReceiver, filter);        }        public class MessageReceiver extends BroadcastReceiver {            @Override            public void onReceive(Context context, Intent intent) {                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {                    messge = intent.getStringExtra(KEY_MESSAGE);                    extras = intent.getStringExtra(KEY_EXTRAS);                    Toast.makeText(getApplicationContext(), "nihao"+extras, Toast.LENGTH_LONG).show();                    StringBuilder showMsg = new StringBuilder();                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");                    showMsg.append(KEY_EXTRAS + " : " + extras + "\n");                    if (!ExampleUtil.isEmpty(extras)) {                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");                        Log.d("JUPSH", "[MyReceiver] 接收Registration Id : " + extras);                    }

// setCostomMsg(showMsg.toString());
}
}
}
还有就是你的推送的消息,分自定义的和系统通知,这要根据自己的需要,自己定义了,
激光官方文档里面说的很清楚了。
这里面主要是androidManifest里面的注册,包名是很重要的,在申请key的时候包名在集成时,不一致的时候是无法推送的,有的android 手机会很智能的给出错误原因,我记得有六个地方包名要一致的。忘了说了,在集成里面其他的设置activity不用管了,就可以了,根据自己的需要做适当的处理。
这里在贴一段我测试的 时候的代码。就是webview 测试,解析推送过来的url的demo的一小段代码。
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
setContentView(web);
// web.getSettings().setDomStorageEnabled(true);
// web.getSettings().setAllowFileAccess(true);
// web.getSettings().setAppCacheEnabled(true);
web.getSettings().setDatabaseEnabled(true);
// Intent intent = getIntent();
// Handler handle=new Handler();
// if (null != intent) {
// final Bundle bundle = getIntent().getExtras();
// handle.post(new Runnable() {
// @Override
// public void run() {
// JSONObject jsonObj = null;
// try {
// jsonObj = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
// loadURL= jsonObj.optString(“url”);
//// web.loadUrl(loadURL);
// com.util.wdt.AppSession.url=loadURL;
// } catch (JSONException e) {
// e.printStackTrace();
// }
// }
// });
// }
web.loadUrl();在这里面就是解析出来的url在适当的时候做链接用,
对于项目里面可不能这样简单的实现,js端必须写一个方法,让android 端做一个回调 就是javascript:() 后面就是方法,让js端做自己想要的处理。这样是极好的,还有一个问题就是:
在主activity里面做url的跳转和链接会报各种错误,有handle 处理还是会有错误,这个和线程有关,什么异步加载了,有着浪费时间,对了android 一个小的框架eventbus对界面刷新了,做了很好的处理,扯得有点远了,我的实现,实在住activity的前面又加了一个activity的,在他面前注入MessageReceiver。这样问题就迎刃而解,对于隐形的activity的无痕跳转,你只用注册一个这个就ok:

@style/noAnimation
true
true
false
@android:color/transparent

<style name="noAnimation">    <item name="android:activityOpenEnterAnimation">@null</item>    <item name="android:activityOpenExitAnimation">@null</item>    <item name="android:activityCloseEnterAnimation">@null</item>    <item name="android:activityCloseExitAnimation">@null</item>    <item name="android:taskOpenEnterAnimation">@null</item>    <item name="android:taskOpenExitAnimation">@null</item>    <item name="android:taskCloseEnterAnimation">@null</item>    <item name="android:taskCloseExitAnimation">@null</item>    <item name="android:taskToFrontEnterAnimation">@null</item>    <item name="android:taskToFrontExitAnimation">@null</item>    <item name="android:taskToBackEnterAnimation">@null</item>    <item name="android:taskToBackExitAnimation">@null</item></style>

这个实在这个里面引用的:androidManifest

0 0
原创粉丝点击