Android GCM基本实现

时间:2012-07-11 17:48:17

标签: android google-cloud-messaging

更新:我修复了以下代码中的问题,因此这是一个很好的基本工作示例,说明如何使用GCM


所以,我正在尝试在我的应用中实施Android GCM。以下是我添加到清单中的相关部分:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />

<permission
    android:name=".permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name=".permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

...

<receiver
    android:name="com.google.android.gcm.GCMBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.badbob.app.gmctestapp" />
    </intent-filter>
</receiver>

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

我已将以下代码添加到我的主要活动的onCreate中:

    GCMRegistrar.checkDevice( this );
    GCMRegistrar.checkManifest( this );
    final String regId = GCMRegistrar.getRegistrationId( this );
    if( regId.equals( "" ) ) {
        GCMRegistrar.register( this, GCM_SENDER_ID );
    }
    else {
        Log.v( LOG_TAG, "Already registered" );
    }

我也像这样创建了GCMIntenetService类:

public class GCMIntentService extends GCMBaseIntentService {

    private static final String LOG_TAG = "GetAClue::GCMIntentService";

    public GCMIntentService() {
        super( GCM_SENDER_ID );
        // TODO Auto-generated constructor stub
        Log.i( LOG_TAG, "GCMIntentService constructor called" );
    }

    @Override
    protected void onError( Context arg0, String errorId ) {
        // TODO Auto-generated method stub
        Log.i( LOG_TAG, "GCMIntentService onError called: " + errorId );
    }

    @Override
    protected void onMessage( Context arg0, Intent intent ) {
        // TODO Auto-generated method stub
        Log.i( LOG_TAG, "GCMIntentService onMessage called" );
        Log.i( LOG_TAG, "Message is: " + intent.getStringExtra( "message" ) );
    }

    @Override
    protected void onRegistered( Context arg0, String registrationId ) {
        // TODO Auto-generated method stub
        Log.i( LOG_TAG, "GCMIntentService onRegistered called" );
        Log.i( LOG_TAG, "Registration id is: " + registrationId );
    }

    @Override
    protected void onUnregistered( Context arg0, String registrationId ) {
        // TODO Auto-generated method stub
        Log.i( LOG_TAG, "GCMIntentService onUnregistered called" );
        Log.i( LOG_TAG, "Registration id is: " + registrationId );
    }
}

当我运行此操作时,我会在 LogCat

中获得此信息
07-11 11:28:46.340: V/GCMRegistrar(27435): Registering receiver
07-11 11:28:46.370: D/GCMRegistrar(27435): resetting backoff for com.badbob.app.getacluebeta
07-11 11:28:46.380: V/GCMRegistrar(27435): Registering app com.badbob.app.getacluebeta of senders 128205395388

根据我从其他帖子中收集到的内容,我应该在LogCat中获得注册ID,但我不是。onRegistered()中的GCMIntentService永远不会被调用。那么我是什么?做错了?

9 个答案:

答案 0 :(得分:18)

这是不正确的

protected GCMIntentService( String senderId ) {         
super(senderId);}

正如文档中所述。你必须为GCMIntentService声明一个PUBLIC,NO ARGUMENT构造函数。否则,GCMIntentService无法由后台意图服务正确实例化。

public GCMIntentService(){
super(senderID);}

senderID可以是硬编码的常量字符串,因为它不再随新的GCM而改变。 使用正确的senderID也非常重要。 24小时足以让你自己活跃,所以如果我的上述解决方案无效,你使用的是不正确的senderID。其他一切看起来都很棒。

当您浏览Google API访问权限页时,senderID位于您的网络浏览器的网址中。单击GCM,浏览器URL中将显示一个12位数字。这是正确使用的密钥。不是你的API密钥。这是在App服务器端使用的。

答案 1 :(得分:8)

我也有这个恼人的错误,直到现在。问题是,我在另一个包中声明了intentservice ...即使我在android清单中声明了名称并且没有放置classnotfound异常......也没有找到任何错误......所以我确保了intentservice在根包中。

答案 2 :(得分:3)

我认为您在代码中遇到了一些问题。

您应该在清单文件中注册您自己的广播接收器,该接收器将触发<service android:name=".GCMIntentService" />

因此,你必须做我写下面的事情。

  • 接收方必须声明为:

    <receiver
        android:name="your.package.name.YourBroadCastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        </intent-filter>
    

  • 将启动该服务的广播接收器。

    public class YourBroadCastReceiver extends BroadcastReceiver {
    
    @Override
     public final void onReceive(Context context, Intent intent) {
         GCMIntentService .runIntentInService(context, intent);
         setResult(Activity.RESULT_OK, null, null);
     }  
    }
    

我建议你看看官方GCM documentation,你可以在那里找到一个很好的例子。

并且...不要忘记在主Google API控制台页面中启用 Google Cloud Messaging 服务。

让我知道它是否有帮助!

答案 3 :(得分:3)

答案 4 :(得分:2)

在这里,我已经为如何从头开始获取RegID和通知编写了几个步骤

  1. 在Google Cloud上创建/注册应用
  2. 使用开发设置Cloud SDK
  3. 为GCM配置项目
  4. 获取设备注册ID
  5. 发送推送通知
  6. 接收推送通知
  7. 您可以在以下网址链接中找到完整的教程

      

    Getting Started with Android Push Notification : Latest Google Cloud Messaging (GCM) - step by step complete tutorial

    enter image description here

    代码剪辑以获取注册ID(推送通知的设备令牌)。

    为GCM配置项目


    更新AndroidManifest文件

    要在我们的项目中启用GCM,我们需要在清单文件中添加一些权限 转到AndroidManifest.xml并添加以下代码 添加权限

    <uses-permission android:name="android.permission.INTERNET”/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <uses-permission android:name=“.permission.RECEIVE" />
    <uses-permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE" />
    <permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
    

    添加GCM广播接收器声明

    在应用程序标记中添加GCM广播接收者声明

    <application
            <receiver
                android:name=".GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" ]]>
                <intent-filter]]>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="" />
                </intent-filter]]>
    
            </receiver]]>
    
    <application/>
    

    添加GCM Servie声明

    <application
         <service android:name=".GcmIntentService" />
    <application/>
    

    获取注册ID(推送通知的设备令牌)

    现在转到启动/启动活动

    添加常量和类变量

    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private final static String TAG = "LaunchActivity";
    protected String SENDER_ID = "Your_sender_id";
    private GoogleCloudMessaging gcm =null;
    private String regid = null;
    private Context context= null;
    

    更新OnCreate和OnResume方法

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_launch);
         context = getApplicationContext();
             if (checkPlayServices()) 
         {
                gcm = GoogleCloudMessaging.getInstance(this);
                regid = getRegistrationId(context);
    
                if (regid.isEmpty())
                {
                    registerInBackground();
                }
                else
                {
                Log.d(TAG, "No valid Google Play Services APK found.");
                }
          }
     }
    
    @Override protected void onResume()
    {
           super.onResume();       checkPlayServices();
    }
    
    
    # Implement GCM Required methods (Add below methods in LaunchActivity)
    
    private boolean checkPlayServices() {
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                    GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                            PLAY_SERVICES_RESOLUTION_REQUEST).show();
                } else {
                    Log.d(TAG, "This device is not supported - Google Play Services.");
                    finish();
                }
                return false;
            }
            return true;
     }
    
    private String getRegistrationId(Context context) 
    {
       final SharedPreferences prefs = getGCMPreferences(context);
       String registrationId = prefs.getString(PROPERTY_REG_ID, "");
       if (registrationId.isEmpty()) {
           Log.d(TAG, "Registration ID not found.");
           return "";
       }
       int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
       int currentVersion = getAppVersion(context);
       if (registeredVersion != currentVersion) {
            Log.d(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }
    
    private SharedPreferences getGCMPreferences(Context context) 
    {
        return getSharedPreferences(LaunchActivity.class.getSimpleName(),
                    Context.MODE_PRIVATE);
    }
    
    private static int getAppVersion(Context context) 
    {
         try 
         {
             PackageInfo packageInfo = context.getPackageManager()
                        .getPackageInfo(context.getPackageName(), 0);
                return packageInfo.versionCode;
          } 
          catch (NameNotFoundException e) 
          {
                throw new RuntimeException("Could not get package name: " + e);
          }
    }
    
    
    private void registerInBackground() 
    {     new AsyncTask() {
         Override
         protected Object doInBackground(Object... params) 
         {
              String msg = "";
              try 
              {
                   if (gcm == null) 
                   {
                            gcm = GoogleCloudMessaging.getInstance(context);
                   }
                   regid = gcm.register(SENDER_ID);               Log.d(TAG, "########################################");
                   Log.d(TAG, "Current Device's Registration ID is: "+msg);     
              } 
              catch (IOException ex) 
              {
                  msg = "Error :" + ex.getMessage();
              }
              return null;
         }     protected void onPostExecute(Object result) 
         { //to do here };
      }.execute(null, null, null);
    }
    

    注意:请存储REGISTRATION_KEY,向GCM发送PN消息非常重要 同时保留在我的所有设备中都是唯一的,通过使用此功能,只有GCM会发送推送通知。

    接收推送通知

    添加GCM广播接收器类

    由于我们已在Manifest文件中声明了“GcmBroadcastReceiver.java”,因此我们创建此类 以这种方式更新接收器类代码

    public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) 
        {        ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());        startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
            Toast.makeText(context, “wow!! received new push notification", Toast.LENGTH_LONG).show();
        }
    }
    

    添加GCM服务类

    由于我们已在Manifest文件中声明了“GcmBroadcastReceiver.java”,因此我们创建此类 以这种方式更新接收器类代码

    public class GcmIntentService extends IntentService
    {     public static final int NOTIFICATION_ID = 1;     private NotificationManager mNotificationManager;     private final static String TAG = "GcmIntentService";     public GcmIntentService() {
         super("GcmIntentService");     
         }     @Override
         protected void onHandleIntent(Intent intent) {
              Bundle extras = intent.getExtras();
              Log.d(TAG, "Notification Data Json :" + extras.getString("message"));
    
              GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
              String messageType = gcm.getMessageType(intent);          if (!extras.isEmpty()) {          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                   .equals(messageType)) {
                   sendNotification("Send error: " + extras.toString());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
              .equals(messageType)) {
              sendNotification("Deleted messages on server: "
              + extras.toString());          // If it's a regular GCM message, do some work.
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
              .equals(messageType)) {
              // This loop represents the service doing some work.
              for (int i = 0; i < 5; i++) {
                   Log.d(TAG," Working... " + (i + 1) + "/5 @ "
                   + SystemClock.elapsedRealtime());               try {
                        Thread.sleep(5000);
                   } catch (InterruptedException e) {
                   }
                 }
                 Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                 sendNotification(extras.getString("message"));
               }
            }        // Release the wake lock provided by the WakefulBroadcastReceiver.
            GcmBroadcastReceiver.completeWakefulIntent(intent);
         }     // Put the message into a notification and post it.
         // This is just one simple example of what you might choose to do with
         // a GCM message.
         private void sendNotification(String msg) {          mNotificationManager = (NotificationManager) this
              .getSystemService(Context.NOTIFICATION_SERVICE);
              PendingIntent contentIntent = PendingIntent.getActivity(this, 0,          new Intent(this, LaunchActivity.class), 0);
    
              NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(          this)
              .setSmallIcon(R.drawable.icon)
              .setContentTitle("Ocutag Snap")
              .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
              .setContentText(msg)
              .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    
              mBuilder.setContentIntent(contentIntent);          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
         }
    }
    

答案 5 :(得分:0)

不确定这是否与您的具体问题有关,但是,我遇到了同样的问题。据我所知,一切都设置正确,但onRegistered()处理程序从未被调用过。

我将标准onCreate()onStart()onDestroy()服务方法作为GCMIntentService类的一部分。一旦我删除它们,就会按预期调用处理程序。

我的假设是,通过重写这些方法,我正在绕过所需的代码初始化。

答案 6 :(得分:0)

在阅读ClouDeveloper的帖子后,我删除了onCreate()onDestroy()和其他 onXXX()回调方法。此外,我使用GCMIntentService.onRegistered(Context, regId)调用regId

谢谢ClouDeveloper!

答案 7 :(得分:0)

您还需要在GCMBaseIntentService.onRegistered()回调中注册您的应用服务器。否则,您将无法发送应用消息。

您应该结帐Google's GCM demo client,因为它提供了包含所有正确代码的工作示例。我们还在github上有一个工作样本:airbop-client。它提供了一个稍微更完整的示例,因为它与应用程序服务器进行了工作交互,这对于启用GCM的应用程序是必需的。它适用于AirBop服务器(我帮助创建),最多可免费提供1000台设备。

答案 8 :(得分:0)

如果您没有收到注册的响应,其中一个主要原因是您的清单文件配置不正确 - 尤其是在{{1}中提供“package_name”(您的应用包名称如com.xxx.yyy) }和<permission>正确。