zoukankan      html  css  js  c++  java
  • GCM 实现

    MIME-version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_000_0076_01C29953.BE473C30"; type="text/html" X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 This is a multi-part message in MIME format. ------=_NextPart_000_0076_01C29953.BE473C30 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable Content-Location: file:///~tmp1363935081.TMP.html GCM学习

    GCM学习

     

    google教程

    http://developer.android.com/google/gcm/gs.html

    1.创建项目

    2.Services中开启 Google Cloud Messaging for Android

    3.Create an OAuth 2.0 client ID

    4.API Access 创建 Server Key 和 Android Key

    androidManifest.xml中加入

    <permission

    android:name=3D"com.myb.app.permission.C2D_MESSAGE"

    android:protectionLevel=3D"signature" />

    <uses-permission android:name=3D"com.myb.app.permission.C2D_MESSAGE" />

     

    //其中com.myb.app改为相应的包名

    <!-- App receives GCM messages. -->

    <uses-permission android:name=3D"com.google.android.c2dm.permission.RECEIVE" />

    <!-- GCM connects to Google Services. -->

    <uses-permission android:name=3D"android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->

    <uses-permission android:name=3D"android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->

    <uses-permission android:name=3D"android.permission.WAKE_LOCK" />

     

    <receiver

    android:name=3D"com.google.android.gcm.GCMBroadcastReceiver"

    android:permission=3D"com.google.android.c2dm.permission.SEND" >

    <intent-filter>

    <action android:name=3D"com.google.android.c2dm.intent.RECEIVE" />

    <action android:name=3D"com.google.android.c2dm.intent.REGISTRATION" />

    <category android:name=3D"com.myb.app" />

    </intent-filter>

    </receiver>

     

     

     

    <service android:name=3D".GCMIntentService" />

     

    android SdK extras\google\gcm\gcm-client\dist 找到 gcm.jar,加入项目中

     

    google服务器注册服务

     

    /***

     * google注册服务

     */

    private void GcmRegistrarDevice() {

    GCMRegistrar.checkDevice(this);

    GCMRegistrar.checkManifest(this);

    final String regId =3D GCMRegistrar.getRegistrationId(this);

    if (regId.equals("")) {

      GCMRegistrar.register(this, SENDER_ID);

    } else {

      Log.v(TAG, "Already registered" + regId);

    }

    }

     

    SENDER_ID 为创建的google项目的 project ID

    创建 GCMIntentService 类,从 GCMBaseIntentService 继承 接受注册的GCM发回的信息

     

    package com.myb.app;

     

    import android.app.Notification;

    import android.app.NotificationManager;

    import android.app.PendingIntent;

    import android.content.Context;

    import android.content.Intent;

    import android.util.Log;

     

    import com.google.android.gcm.GCMBaseIntentService;

     

    public class GCMIntentService extends GCMBaseIntentService{

    private final static String TAG =3D GCMIntentService.class.getSimpleName();

     

    private NotificationManager mNM;

     

    @Override

    public void onCreate() {

    super.onCreate();

     

    mNM =3D (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    }

     

    @Override

    protected void onError(Context arg0, String arg1) {

    Log.d(TAG, "onError:" + arg1);

    }

     

    @Override

    protected void onMessage(Context arg0, Intent intent) {

    Log.e("GCM MESSAGE", "Message Recieved!!!");

            String message = intent.getStringExtra("message");

            if (message == null) {

                Log.e("NULL MESSAGE", "Message Not Recieved!!!");

            } else {

                Log.i(TAG, "new message=3D " + message);

                showNotifiy(message);

            }

    }

     

    @Override

    protected void onRegistered(Context arg0, String arg1) {

    Log.d(TAG, "onRegistered:" + arg1);

    }

     

    @Override

    protected void onUnregistered(Context arg0, String arg1) {

    Log.d(TAG, "onUnregistered:" + arg1);

    }

     

     

    private void showNotifiy(String msg){

    Intent intent =3D new Intent(this, HomeActivity.class);

    PendingIntent contentIntent =3D PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    String tickerText =3D getString(R.string.app_name, msg);

     

    // construct the Notification object.

    Notification notif = new Notification(R.drawable.ic_logo, tickerText, System.currentTimeMillis());

    notif.setLatestEventInfo(this, "母婴宝", msg, contentIntent);

    notif.defaults = Notification.DEFAULT_ALL;

    mNM.notify(R.string.app_name, notif);

    }

     

    }

     

    后台,我们的后台使用的是maven+spring mvc+jpa, 如果不是maven管理项目则把Android SDK 下面的xtras\google\gcm\gcm-server\dist 中的 gcm-server.jar加入项目。如果是mavengithub上有人做了处理

     

    https://github.com/slorber/gcm-server-repository

     

     

    pom.xml中加入

     

     

    <repository>

        <id>gcm-server-repository</id>

        <url>https://raw.github.com/slorber/gcm-server-repository/master/releases/</url>

    </repository>

     

    <dependency>

        <groupId>com.google.android.gcm</groupId>

        <artifactId>gcm-server</artifactId>

        <version>1.0.2</version>

    </dependency>

     

    向注册的设备发送消息

     

    Sender sender = new Sender(myApiKey);

    Message message = new Message.Builder().addData("message", msg).build();

    MulticastResult mr = sender.send(message, devices, 5);

    for(Result result : mr.getResults()){

      if (result.getMessageId() != null) {

        String regId = result.getCanonicalRegistrationId();

        String error = result.getErrorCodeName();

        if (error.equals(Constants.ERROR_NOT_REGISTERED)) {

          // application has been removed from device - unregister

          // database

          phoneDao.delete(regId);

        }

      }

    }

     

    当前项目的API key

    devices 用户设备的注册ID,在客户端可以得到,这个值应该保存到后台

     

  • 相关阅读:
    《敏捷开发修炼之道》学习笔记3:交付用户想要的软件
    Photoshop快捷键大集合
    如何制作已编译的HTML帮助文件(即CHM帮助文件)
    根本不存在 DIV + CSS 布局这回事
    可将视频转换成Gif动画的相关软件
    SEO是什么?与spam有什么区别呢?
    视频六大编辑软件大比拼
    陈彤:一个网络编辑的11年
    最近出现的p2psvr.exe恶意程序的解决办法
    使用火狐浏览器Firefox的一些小技巧
  • 原文地址:https://www.cnblogs.com/warrior/p/2975602.html
Copyright © 2011-2022 走看看