android GCM实现

时间:2012-08-21 08:49:51

标签: android google-cloud-messaging

我想实施谷歌云消息服务CCM。但我得到强制关闭错误。我正在调试真实设备上的代码。  我的Main.java代码如下:

package com.imran.fgcm;

import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "123450000");
          Toast.makeText(this, regId , Toast.LENGTH_LONG).show();
        } else {
        // Log.v(TAG, "Already registered");
            Toast.makeText(this, "Already registered" , Toast.LENGTH_LONG).show();
        }

我的android清单文件代码如下:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imran.fgcm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
    <permission android:name="com.imran.fgcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.imran.fgcm.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

  <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.imran.fgcm" />
  </intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
    </application>

</manifest>

现在它在我添加了GCMIntetservice.java类后停止了强制关闭错误。但它仍然没有改变设备ID。我的logcat输出是

01-03 11:34:01.023: V/GCMRegistrar(6469): Registering receiver
01-03 11:34:01.023: D/GCMRegistrar(6469): resetting backoff for com.imran.fgcm
01-03 11:34:01.058: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:01.288: D/TextLayoutCache(6469): Using debug level: 0 - Debug Enabled: 0
01-03 11:34:01.843: D/CLIPBOARD(6469): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:34:03.138: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:03.138: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:03.138: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:03.143: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-1
01-03 11:34:03.148: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:03.148: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:03.148: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 4143 (3000)
01-03 11:34:03.213: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.318: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:07.318: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:07.318: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:07.353: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-2
01-03 11:34:07.368: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:07.398: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.443: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:07.443: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:07.443: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:07.453: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-3
01-03 11:34:07.458: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:07.458: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:07.458: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 7873 (6000)
01-03 11:34:07.503: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.508: D/dalvikvm(6469): GC_CONCURRENT freed 213K, 4% free 12852K/13319K, paused 3ms+3ms
01-03 11:34:15.343: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:15.343: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:15.343: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:15.378: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-4
01-03 11:34:15.393: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:15.428: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:15.498: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:15.498: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:15.498: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:15.503: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-5
01-03 11:34:15.508: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:15.508: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:15.508: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 7347 (12000)
01-03 11:34:15.538: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:22.863: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:22.863: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:22.863: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:22.908: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-6
01-03 11:34:22.923: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:22.958: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:22.968: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:22.968: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:22.968: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:22.978: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-7
01-03 11:34:22.978: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:22.983: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:22.983: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 35123 (24000)
01-03 11:34:23.013: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:58.113: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:58.118: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:58.118: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:58.153: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-8
01-03 11:34:58.168: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:58.203: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:58.228: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:58.228: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:58.228: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:58.233: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-9
01-03 11:34:58.238: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:58.238: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:58.238: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 67761 (48000)
01-03 11:34:58.268: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:35:50.863: D/CLIPBOARD(6469): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:35:52.688: E/ActivityThread(6469): Activity com.imran.fgcm.Main has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41903ec8 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-03 11:35:52.688: E/ActivityThread(6469): android.app.IntentReceiverLeaked: Activity com.imran.fgcm.Main has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41903ec8 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1118)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1105)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:355)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.google.android.gcm.GCMRegistrar.setRetryBroadcastReceiver(GCMRegistrar.java:293)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:215)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.imran.fgcm.Main.onCreate(Main.java:19)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.Activity.performCreate(Activity.java:4470)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.os.Looper.loop(Looper.java:137)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.main(ActivityThread.java:4511)
01-03 11:35:52.688: E/ActivityThread(6469):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:35:52.688: E/ActivityThread(6469):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-03 11:35:52.688: E/ActivityThread(6469):     at dalvik.system.NativeStart.main(Native Method)
01-03 11:36:06.028: V/GCMRegistrar(6860): Registering receiver
01-03 11:36:06.033: D/GCMRegistrar(6860): resetting backoff for com.imran.fgcm
01-03 11:36:06.053: V/GCMRegistrar(6860): Registering app com.imran.fgcm of senders 732773857047
01-03 11:36:06.093: D/TextLayoutCache(6860): Using debug level: 0 - Debug Enabled: 0
01-03 11:36:06.178: D/CLIPBOARD(6860): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:36:06.203: V/GCMBroadcastReceiver(6860): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:36:06.203: V/GCMBroadcastReceiver(6860): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:36:06.203: V/GCMBaseIntentService(6860): Acquiring wakelock
01-03 11:36:06.218: V/GCMBaseIntentService(6860): Intent service name: GCMIntentService-732773857047-1
01-03 11:36:06.238: D/GCMBaseIntentService(6860): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:36:06.238: D/GCMBaseIntentService(6860): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:36:06.238: D/GCMBaseIntentService(6860): Scheduling registration retry, backoff = 2363 (3000)
01-03 11:36:06.263: V/GCMBaseIntentService(6860): Releasing wakelock
01-03 11:36:08.618: V/GCMBroadcastReceiver(6860): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:36:08.618: V/GCMBroadcastReceiver(6860): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:36:08.618: V/GCMBaseIntentService(6860): Acquiring wakelock
01-03 11:36:08.653: V/GCMBaseIntentService(6860): Intent service name: GCMIntentService-732773857047-2
01-03 11:36:08.668: E/GCMBaseIntentService(6860): Received invalid token: 1111101101100100100110110001110011001001010011110110001010010110
01-03 11:36:08.668: V/GCMBaseIntentService(6860): Releasing wakelock

3 个答案:

答案 0 :(得分:2)

用于GCM实施。创建一个新项目并将GCM.jar文件添加到其buildpath。按照以下方式生成Manifiest文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR PACKAGE NAME"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission
        android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
   <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".DemoGCMPush"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <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="YOUR PACKAGE NAME" />
            </intent-filter>
        </receiver>

        <service android:name="YOUR PACKAGE NAME.GCMIntentService" />
    </application>

</manifest>

然后在您的主要活动中添加以下代码进行注册。将SENDER_ID替换为URL中的12位项目ID。

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

最后添加第二类GCMIntentService.java来处理GCM消息。

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

/**
 * {@link IntentService} responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {

    @SuppressWarnings("hiding")
    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super("SENDER ID");
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context,
                SamplePushActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("message"));
    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

}

答案 1 :(得分:1)

您是否正确地在GCMIntentService类中添加了项目ID。另外,请确保该设备已安装Android电子市场/ Play商店,并使用Google帐户登录。

答案 2 :(得分:1)

要接收通知,您需要从GCMIntentService类重写onMessage方法,如下所示

@Override
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("**mymsg**"));
    }

这里&#34; mymsg&#34;我应该与您的PHP消息属性匹配。请注意,两个名称在php和android中都应该相同,否则你将不会收到通知。

相关问题