Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 4.0.4(SDK15)中未收到GCM推送通知_Android_Google Cloud Messaging - Fatal编程技术网

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

Android 4.0.4(SDK15)中未收到GCM推送通知,android,google-cloud-messaging,Android,Google Cloud Messaging,在我当前的项目中,我们使用由远程服务器发送的推送通知。对于Android SDK16+,它们工作正常,但对于4.0.4版SDK15,它们根本没有收到。事实上,在gcminentservice中,永远不会调用回调挂钩。 为了测试这一点,我根据官方教程在 这是舱单: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.example">

在我当前的项目中,我们使用由远程服务器发送的推送通知。对于Android SDK16+,它们工作正常,但对于4.0.4版SDK15,它们根本没有收到。事实上,在gcminentservice中,永远不会调用回调挂钩。 为了测试这一点,我根据官方教程在

这是舱单:

<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和GCMinentService与提供的教程完全相同。
知道为什么通知没有到达吗?

发生了一些奇怪的事情。我使用的平板电脑有一个Gmail帐户登录。我添加了自己的Gmail,之后就可以收到通知了。然后,我从平板电脑上删除了我的Gmail帐户,只留下了原来的Gmail,它继续工作
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();
    }

}