基于Windows Azure服务器的GCM推送——Android端集成
2015-11-19 14:55
495 查看
项目中用到消息推送,要用到Google的GCM推送,就简单的记录一下:
参考地址:http://www.windowsazure.cn/documentation/services/mobile-services/
一、
下载并导入需要的jar包:mobileservices-2.0.3.jar 、 guava-17.0.jar 、 notifications-1.0.1.jar
二、
在启动的Activity中加入以下代码:
其中PushAppUrl 是服务端集成给出的
PushAppKey 是在Google控制台申请的
三、注册并接受推送过来的消息
四、最后别忘了在配置AndroidManifest.xml信息
添加需要的权限和
具体操作参见文章开头的网址
参考地址:http://www.windowsazure.cn/documentation/services/mobile-services/
一、
下载并导入需要的jar包:mobileservices-2.0.3.jar 、 guava-17.0.jar 、 notifications-1.0.1.jar
二、
在启动的Activity中加入以下代码:
其中PushAppUrl 是服务端集成给出的
PushAppKey 是在Google控制台申请的
<span style="font-size:14px;"> public static MobileServiceClient mClient; try { mClient = new MobileServiceClient(Constants.PushAppUrl, Constants.PushAppKey, this); } catch (MalformedURLException e) { e.printStackTrace(); }</span>
三、注册并接受推送过来的消息
NotificationsManager.handleNotifications(getActivity(), Constants.SENDER_ID, MyNotificationHandler.class);
public class MyNotificationHandler extends NotificationsHandler { private NotificationManager mNotificationManager; NotificationCompat.Builder builder; Context ctx; @Override public void onReceive(Context context, Bundle bundle) { ctx = context; String nhMessage = bundle.getString("Notification"); PushMsgModel pModel = JsonHelper.getGson().fromJson(nhMessage, PushMsgModel.class); if (!pModel.getBody().getTag().equalsIgnoreCase(SPUtils.getPushTag())) { return; } sendNotification(context, pModel); } @Override public void onRegistered(final Context context, final String gcmRegistrationId) { super.onRegistered(context, gcmRegistrationId); new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... params) { try { String[] tags = {ShareData.CurrPushTag}; MainActivity.mClient.getPush().register(gcmRegistrationId, tags); return null; } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(); } private void sendNotification(Context context, PushMsgModel pModel) { mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(pModel.getTitle()) .setStyle(new NotificationCompat.BigTextStyle() .bigText(pModel.getHeader())) .setAutoCancel(true) .setContentText(pModel.getHeader()); mNotificationManager.notify(0, mBuilder.build()); }
四、最后别忘了在配置AndroidManifest.xml信息
添加需要的权限和
<!-- push --> <receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.waitur.app.customer" /> </intent-filter> </receiver>
具体操作参见文章开头的网址
相关文章推荐
- Android 开发之RxJava 详解
- Android Studio导入Fresco 和 简单使用
- Android Glide 图片加载
- FaceBook推出的Android图片加载库-Fresco
- Android中asset文件夹和raw文件夹区别
- Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用]
- Android界面View及ViewGroup
- Android学习笔记:如何设置ImageView中图片的显示方式
- Android工具类 — Volley的使用
- android Canvas中的clipRect、drawBitmap
- Android Activity启动模式详解
- 解决android SwipeRefreshLayout recyclerview 不能下拉
- Android Canvas 说明
- Android控件之CheckBox、RadioButton
- android定制 Launcher
- android 5.0 新特性
- Android音频系统之音频框架
- android四大组件之Service 播放音乐
- 【Android】长按连续触发事件的实现方法
- Android编程实现仿心跳动画效果的方法