无法使用GCM向Android发送推送

时间:2016-02-16 13:39:14

标签: java android google-cloud-messaging

抱歉我的英文。我花了2天但我无法向Android发送推送消息。我使用谷歌云消息。例如在gcm中我创建了一个新项目,我有一个id:

enter image description here

然后我启用了gcm并添加了服务器密钥

enter image description here

我有这样的代码:

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alexy.gcmclient">

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

    <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.hmkcode.android.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.hmkcode.android.gcm.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >

        <activity
            android:name=".MainActivity"
            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=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.hmkcode.android.gcm" />
            </intent-filter>
        </receiver>

        <service
            android:name=".GcmMessageHandler"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
  </application>


</manifest>

GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("GcmBroadcastReceiver", "GcmBroadcastReceiver");
        // Explicitly specify that GcmMessageHandler will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmMessageHandler.class.getName());

        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmMessageHandler

public class GcmMessageHandler extends IntentService {

    String mes;
    private Handler handler;
    public GcmMessageHandler() {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        handler = new Handler();
        Log.e("GcmMessageHandler", "GcmMessageHandler");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        Log.e("message", messageType);
    }

主要

public class MainActivity extends Activity implements OnClickListener{

    Button btnRegId, unregister;
    EditText etRegId;
    GoogleCloudMessaging gcm;
    String regid;
    String PROJECT_NUMBER = "308****";

    private BroadcastReceiver mRegistrationBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        unregister = (Button) findViewById(R.id.unregister);
        btnRegId = (Button) findViewById(R.id.btnGetRegId);
        etRegId = (EditText) findViewById(R.id.etRegId);

        btnRegId.setOnClickListener(this);


    }

    public void getRegId(){
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";

                try {

                    InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
                    String token = instanceID.getToken(PROJECT_NUMBER,
                            GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

                    msg = token;
                    //apiKey = msg;
                    GcmPubSub.getInstance(getApplicationContext()).subscribe(token, "/topics/users", null);

                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }



                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                etRegId.setText(msg + "\n");
                Log.e("key", msg);

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

   @Override
    public void onClick(View v) {
        getRegId();
    }

}

我使用service并尝试向设备发送一些推送消息。在服务中它说成功。但是android中的推送没有到来(我在日志中没有输出)

2 个答案:

答案 0 :(得分:1)

C2D_MESSAGE的软件包名称错误,请将其更改为软件包名称。此外,请确保您的app目录的build.gradle附近有配置文件。编辑:浏览所有清单并更改hmk到您的包名称的任何位置。根据您的清单,您的包名称应为com.example.alexy.gcmclient。

答案 1 :(得分:1)

我认为您的应用包名称必须为 com.example.alexy.gcmclient 或替换包名称build.gradle

defaultConfig 
        applicationId "com.yourpackge"

<permission
    android:name="com.example.alexy.gcmclient.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission
    android:name="com.example.alexy.gcmclient.permission.C2D_MESSAGE" />



   <receiver android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
              <intent-filter>
                    <action       android:name="com.google.android.c2dm.intent.REGISTRATION" />

                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                    <category android:name="com.example.alexy.gcmclient" />
                </intent-filter>
            </receiver>