Android GCM基本实现

28

更新:我已经修复了下面代码中的问题,这样就可以很好地演示如何使用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,但我没有。此外,在GCMIntentServiceonRegistered()从未被调用。那么我做错了什么?


你能展示一下你的GCMBroadcastReceiver和GCMRegistrar类的代码吗? - yugidroid
哦,我还没有写那些类。我发布的就是我写的全部代码。我漏掉了什么吗? - Rooster242
@SevaAlekseyev,非常感谢。没错。但是为什么Rooster242注册了名为“com.google.android.gcm.GCMBroadcastReceiver”的接收器呢?他必须注册一个扩展BrodcastReceiver的所需广播类,以触发“.GCMIntentService”服务。 - yugidroid
据我所知,GCM库会为您处理这个问题,这就是为什么该类必须被命名为GCMIntentService的原因。这不正确吗? - Rooster242
@Rooster242:GCM_SENDER_ID 在哪里被定义/赋值了? - capdragon
显示剩余3条评论
9个回答

18

这是不正确的

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

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

public GCMIntentService(){
super(senderID);}

由于新的GCM不再更改senderID,因此senderID可以是一个硬编码的常量字符串。同时使用正确的senderID非常重要。您的senderID在24小时内足够活跃,所以如果我的上述解决方案无效,则您可能正在使用不正确的senderID。其他所有内容都看起来很好。

当您浏览Google API访问页面时,senderID位于浏览器URL中。点击GCM,浏览器URL中会出现一个12位数字。那是要使用的正确密钥。不是您的API密钥,后者用于应用服务器端。


不,你不需要项目ID。抱歉,我漏掉了一些东西。 你应该尝试在清单文件中添加一个类别标签,其中包含你的应用程序包名称。<catagory android:name="yourAppPackageHere"> - Ryan Gray
我也尝试将GCMIntentService的所有其他函数设置为public,但这也没有解决问题。 - Rooster242
1
从文档中得知:“对于针对minSdkVersion 16及更高版本的应用程序,类别标签不是必需的。” - Rooster242
1
文档是错的!我把 <category> 标签(这次放在正确的位置)加上后,它就开始工作了。感谢 Ryan,你发现了我的代码中的两个问题。 - Rooster242
2
文档说明如果'min_sdk'为16,则可以省略它。但是OP正在使用'min_sdk'=8。 - tomi
显示剩余2条评论

8

我之前也遇到了这个非常烦人的bug,直到现在才解决。问题是我把IntentService声明在另一个包里...即使我在Android Manifest中声明了名称,并且没有出现ClassNotFound异常...根本找不到任何错误...所以我确保将IntentService放在了根包中。


谢谢你,我也遇到了同样的问题,GCMIntentService类在另一个包中,无法响应注册意图。你说得对,这个bug非常恼人。 - connoisseur


3

我认为你的代码存在一些问题。

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

因此,你必须像下面我写的那样做。

  • The receiver must be declared like:

    <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>
    

  • Your broadcast receiver that will start the service.

    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 文档,其中有一个很好的示例。

还有...不要忘记在主Google APIs控制台页面上启用 Google云消息传递服务。

如果有帮助,请告诉我!


我理解你的观点,但我相信GCM库已经为你处理了那个问题。摘录自官方文档(顺便说一下,我已经看了两天):“这个Intent服务将会被GCMBroadcastReceiver调用(由GCM库提供),如下一步所示。它必须被命名为my_app_package.GCMIntentService,除非你使用一个GCMBroadcastReceiver的子类来覆盖用于命名服务的方法。”所以我尝试了你的建议,但是runIntentInService是一个私有方法(需要3个参数而不是2个)。 - Rooster242
为了确保稳定性,我进行了双重检查,确认 GCM 在 Google API 中已启用。 - Rooster242

2

我为如何从零开始获取RegID和通知编写了几个步骤

  1. 在Google Cloud上创建/注册应用程序
  2. 设置带有开发的Cloud SDK
  3. 为GCM配置项目
  4. 获取设备注册ID
  5. 发送推送通知
  6. 接收推送通知

您可以在下面的URL链接中找到完整的教程

入门Android推送通知:最新的Google云端消息传递(GCM)-逐步完整教程

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广播接收器声明。
<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服务声明
<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广播接收器类

由于我们已经在清单文件中声明了“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服务类

由于我们已经在清单文件中声明了“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());
     }
}

0

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

你真的应该查看一下Google的GCM演示客户端,因为它提供了一个带有所有正确代码的工作示例。我们在github上也有一个工作示例:airbop-client。它提供了一个稍微更完整的示例,因为它具有与应用服务器的工作交互,这对于启用GCM的应用程序是必要的。它可以与AirBop服务器配合使用(我曾经帮助创建),免费支持1000个设备。


0
如果您无法从注册中获得响应,主要原因之一是您的清单文件未正确配置,尤其是在和中正确提供“package_name”(您的应用程序包名称,如com.xxx.yyy)。

0

不确定这是否与您的特定问题有关,但我经历了同样的问题。 我认为一切都设置正确了,但是onRegistered()处理程序从未被调用过。

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

我的假设是通过覆盖这些方法,我绕过了所需的代码初始化。


我没有定义那些函数,但知道这一点很好。我完全被卡住了。 - Rooster242

0
阅读了ClouDeveloper的帖子后,我删除了onCreate()onDestroy()和其他onXXX()回调方法。此外,我的GCMIntentService.onRegistered(Context, regId)被调用并传入了regId参数。
感谢ClouDeveloper!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接