Android 4.0.4(SDK15)中未收到GCM推送通知

时间:2014-11-03 16:37:45

标签: android google-cloud-messaging

在我当前的项目中,我们使用的是由远程服务器发送的推送通知。它们适用于Android SDK16 +,但对于4.0.4版本(SDK15),它们根本不会被接收。实际上,在GcmIntentService中,永远不会调用回调挂钩。 为了测试这个,我按照官方教程创建了一个示例应用程序 https://developer.android.com/google/gcm/client.html

这是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.example">

    <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="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.mycompany.example.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.mycompany.example.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".AladdinMainActivity"
            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=".receiver.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.mycompany.example" />
            </intent-filter>
        </receiver>
        <service android:name=".service.GcmIntentService" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    </application>

</manifest>

主要活动只是所需功能的复制粘贴。 GcmBroadcastReceiver和GcmIntentService与提供的教程完全相同。 任何有关通知未到达的想法?

主要活动只是所需功能的复制粘贴。

package com.mycompany.example;

public class AladdinMainActivity extends Activity {

    private static final String TAG = AladdinMainActivity.class.getSimpleName();
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private static final String SENDER_ID = "my_sender_id";
    public static final String ERROR_PLAY_SERVICES = "Can't use Play services!";
    public static final String PROPERTY_REG_ID = "registration_id";     // TODO
    private static final String PROPERTY_APP_VERSION = "1";    // TODO

    private GoogleCloudMessaging gcm;
    private Context context;
    private String regid;
    private TextView mDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aladdin_main);
        mDisplay = (TextView) findViewById(R.id.display);
        context = getApplicationContext();
        showErrorUnlessCheckedPlayServices();
    }

    private void showErrorUnlessCheckedPlayServices() {
        if (checkPlayServices()) {
            this.gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);
            if (regid.isEmpty()) {
                registerInBackground();
            }
            mDisplay.setText(regid);
        } else {
            Toast.makeText(this.getApplicationContext(), ERROR_PLAY_SERVICES,
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        showErrorUnlessCheckedPlayServices();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.aladdin_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    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.i(TAG, "This device is not supported.");
                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.i(TAG, "Registration not found.");
            return "";
        }
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }

    private SharedPreferences getGCMPreferences(Context context) {
        return getSharedPreferences(AladdinMainActivity.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);
                    msg = "Device registered, registration ID=" + regid;
                    sendRegistrationIdToBackend();
                    storeRegistrationId(context, regid);
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }

        }.execute(null, null, null);
    }

    // TODO delete this method, it's not going to be implemented (no server)
    private void sendRegistrationIdToBackend() {}

    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }

}

GcmBroadcastReceiver和GcmIntentService与提供的教程完全相同。 任何有关通知未到达的想法?

0 个答案:

没有答案