Using GCM in android studio

2019-06-17 10:33发布

问题:

I don't know how to get GCM working in my project in android studio.

When I use some of the clases of GCM, y get this: Can't resolve symbol GCMRegistrar In this code:

GCMRegistrar.checkDevice(this);

And it does not show the option to import the class.

This is my graddle file:

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.0'

    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile 'com.google.android.gms:play-services:4.2.42'
}

This is my manifest:

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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />

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

    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/icono_app"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.compage.womenshield.Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCgQNclD9ZqxhiJXGdFvTTx2OiR_vKfJCM"/>

        <receiver
            android:name=".GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.javapapers.android" />
            </intent-filter>
        </receiver>

        <service android:name="com.compage.womenshield.notification.GCMIntentService" />

    </application>

</manifest>

It shows error in here:

<receiver
                android:name=".GCMBroadcastReceiver"

In the clase name. And here:

<service android:name="com.compage.womenshield.notification.GCMIntentService" />

How can I use these gcm classes in android studio?

回答1:

You are probably using an old tutorial but GCMRegistrar is a deprecated API class.

You should really be using the new one, as it will be removed soon.

Please use the GoogleCloudMessaging API instead.



回答2:

You are using older way. you should check official documentation of google to use gcm in your app. now Instance ID is used to get token

String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

for complete tutorial you can check tutorial