先来张图先
相信很多人想知道,类似微信,扣扣,或者网易新闻的消息推送是怎样实现的,对于初学者来说,可能感觉只能“远观而不可亵玩焉”,或者说,想做,但是不知道怎么做、怎么实现,那么,在这里,恭喜你,即使你是初学者,知道四大组件,那么你也可以做推送啦。有木有很激动~
首先呢,推送我们要用到第三方,那么推送的第三方有很多,比如bmob推送啊,极光推送啊等等,在这里就不多介绍第三方的推送包啦,那么,在这里我就选择bmob推送来给大家讲解怎么实现推送,相对来说还是比较简单易懂的。
首先我们先去Bmob官网注册一个帐号,注册很简单的,邮箱密码就可以了, 然后创建应用,当然,bmob有很多功能,比如移动云数据库、云代码,聊天组件和支付组件等等,这些我就不说了,有兴趣的自己看相关内容或者文档吧,我们直接跳到推送这一块。
首先,我们新建一个安卓项目,将安卓的包名保存到你的bmob云服务器上。
对于初学者来说,可能包名不知道是哪个,导致错误(不是初学者可跳过),就在AndroidManifest.xml中查看复制黏贴就行了。
好的,目前,我们的后台开发工作搞定,没啥的。下面就给大家看看我做的推送的效果
首先推送的数据是json数据,如果你看过代码后,你就明白他的原理了,其实,就是通过广播监听,如果监听到了广播,就接受广播出来的信息,而这个信息就是我们要的json数据。
那么我首先把Bmob的sdk和Push的jar包下载拷贝到libs目录中,然后,添加权限:
<permission android:protectionLevel="normal" android:name="cn.bmob.permission.push"></permission> <uses-permission android:name="cn.bmob.permission.push"/><!-- 添加自定义的权限--> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
然后配置清单文件:
<service android:label="PushService" android:name="cn.bmob.push.lib.service.PushService" android:process="cn.bmob.push" android:permission="cn.bmob.permission.push" android:exported="true"> <intent-filter> <action android:name="cn.bmob.push.lib.service.PushService"/> </intent-filter> </service> <receiver android:name="cn.bmob.push.PushReceiver" > <intent-filter android:priority="2147483647" ><!--优先级加最高--> <!-- 系统启动完成后会调用 --> <action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- 解锁完成后会调用 --> <action android:name="android.intent.action.USER_PRESENT" /> <!-- 监听网络连通性 --> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> <receiver android:name="cn.bmober.bmob_push_demo.MyPushMessageReceiver"> <intent-filter > <action android:name="cn.bmob.push.action.MESSAGE"/> </intent-filter> </receiver>
我们在清单文件可以看到cn.bmober.bmob_push_demo.MyPushMessageReceiver,这个是我们自己写的广播接收者,用于广播接收推送内容,这个类有个cn.bmob.push.action.MESSAGE内容的action属性,这个是一定要定义的,这个广播接收的匹配条件,没有这个action是得不到json数据的。
然后我们通过intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING)接受服务端发过来的推送数据,得到数据后,我就定义通知Notification,通知用户有新内容:
public static final int ID = 1001; private NotificationManager mNotification; private Notification notification; private Map<String, String> myMap;
public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) { myMap = parseJson(intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING)); String alert = myMap.get("alert"); String title = myMap.get("title"); String message = myMap.get("message"); String website = myMap.get("website"); mNotification = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notification = new Notification(); notification.icon = R.drawable.bmob; notification.tickerText = alert; notification.when = System.currentTimeMillis(); Intent i = new Intent(context, NotificationAcitvity.class); i.putExtra("website", website); PendingIntent pIntent = PendingIntent.getActivity(context, 0, i,PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, title, message, pIntent); mNotification.notify(ID, notification); }
在上面我们有个parseJson(String json)的方法,这个自己写的解析json数据的方法,也没什么技术含量,就直接上代码了:
/** * 解析推送过来的json数据 * * @param json * @return */ private Map<String, String> parseJson(String json) { Map<String, String> map = new HashMap<String, String>(); try { JSONObject jsonObject = new JSONObject(json); map.put("alert", jsonObject.getString("alert")); map.put("title", jsonObject.getString("title")); map.put("message", jsonObject.getString("message")); map.put("website", jsonObject.getString("website")); } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e); } return map; }
好啦。现在我们得定义NotificationAcitvity.java这个activity,在这个项目中,我就直接打开i推送发过来的网址了,没有做太多复杂的功能。如果推送你公司的活动,就可以发送该活动的网址,如果你的类似于下载助手那样推荐游戏或者app,那么就可以直接跳到app下载页面了。如果你要实现更加有意思的功能,那么就自己好好想想啦。
private NotificationManager notificationMan; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.notification); notificationMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationMan.cancel(MyPushMessageReceiver.ID); Intent intent = getIntent(); String website = intent.getStringExtra("website"); Uri uri = Uri.parse(website); startActivity(new Intent(Intent.ACTION_VIEW,uri)); }
当然,bmob还可以通过代码实现推送任务,这样的话,我们就可以做个手机服务器端,直接编写json数据发送也可以达到一样的效果,就不需要一定要登录bmob云端服务器才能推送数据了,没什么好说的,直接上代码把。
在oncreate中初始化bmob
// 初始化BmobSDK Bmob.initialize(this, APPID); // 使用推送服务时的初始化操作 MyInstallation.getCurrentInstallation(this).save(); // 启动推送服务 BmobPush.startWork(this, APPID); // 创建推送消息的对象 bmobPushManager = new BmobPushManager(this);
public void onClick(View v) { // TODO Auto-generated method stub String pushJsonStr = "{\"alert\":\"吓到我了\",\"title\":\"好大的惊喜呀\",\"message\":\"分享bmob,赠送iPhone 6 plus\",\"website\":\"http://www.bmob.cn\"}"; JSONObject jsonObject = null; try { jsonObject = new JSONObject(pushJsonStr); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } switch (v.getId()) { case R.id.btn_sendAll: // 推送一条消息给所有安装此应用的设备 bmobPushManager.pushMessageAll(jsonObject); break; case R.id.btn_sendMsgToAndroid: // 创建Installation表的BmobQuery对象 BmobQuery<BmobInstallation> query = BmobInstallation.getQuery(); // 并添加条件为设备类型属于android query.addWhereEqualTo("deviceType", "android"); // 设置推送条件给bmobPushManager对象。 bmobPushManager.setQuery(query); // 设置推送消息,服务端会根据上面的查询条件,来进行推送这条消息 bmobPushManager.pushMessage(jsonObject); break; default: break; } }
看完之后是不是觉得其实推送也不过如此呀?是的,其实推送这一块没什么难点,基本知道四大组件就可以做推送这个模块了。至于想通过推送达到什么样的效果,那就得看你的想象和你的实现能力了。
好啦,最好希望写的推送这边文章对你们有帮助。