Android 在带有GCM的设备上接收消息

Android 在带有GCM的设备上接收消息,android,notifications,google-cloud-messaging,Android,Notifications,Google Cloud Messaging,实际上,我正在为Android应用程序编写通知。 我的两个主要信息来源是this和android开发者网站 让我快速描述一下我的应用程序: 在我的应用程序中,我使用web服务(WS),它与POST-HTTP请求一起工作 对于通知,我使用GCM系统 我的应用程序使用上面的“POST HTTP”请求系统在我的服务器上注册 实际上,我的服务器的工作与教程中的完全相同(这意味着在数据库中注册一个帐户,使用两个参数和设备的注册ID,并使用GCM向设备发送一条消息) 实际上,所有这些步骤都有效: My

实际上,我正在为Android应用程序编写通知。 我的两个主要信息来源是this和android开发者网站

让我快速描述一下我的应用程序:
  • 在我的应用程序中,我使用
    web服务
    (WS),它与
    POST-HTTP
    请求一起工作
  • 对于通知,我使用
    GCM
    系统
  • 我的应用程序使用上面的“POST HTTP”请求系统在我的服务器上注册
  • 实际上,我的服务器的工作与教程中的完全相同(这意味着在数据库中注册一个帐户,使用两个参数和设备的注册ID,并使用GCM向设备发送一条消息)
实际上,所有这些步骤都有效:
  • My app receive成功获取注册ID(
    gcm.register(发送者ID);
    work)
  • 我的应用程序在我的服务器上成功注册
  • 当我尝试从设备发送消息时,我的服务器收到一条成功消息

    {“多播id”:6276079906208554309,“成功”:1,“失败”:0,“规范id”:0,“结果”:[{“消息id”:“0:1374826298092960%978fee92f9fd7ecd”}

  • 问题是:
    • 我的设备上没有收到任何信息
    我所做的: 我尝试执行两个版本的应用程序:
    • 首先,我使用教程中的一部分代码制作了一个应用程序,但是使用了
      包com.google.android.gms.gcm.GoogleCloudMessaging
      ,而不是
      com.google.android.gcm
      ,后者已被弃用(并在教程中使用),但我无法收到消息,所以我尝试了第二个版本
    • 这次我学习了整个教程,只是将服务器上的注册功能更改为使用我的,但与第一个应用一样,我的设备上没有收到任何内容。(我这样做是想了解接待处是如何工作的,但这对我没有帮助,现在我将以这种方式忘记)
    我在哪里需要你的帮助: 我需要一些/更多的解释如何接收我的服务器发送的消息

    • 我的主要活动使用android开发者网站上描述的代码
    • 我使用android开发者网站上的代码创建了一个广播类
    • 我没有创建任何IntentService或服务,也许这是我的错误,但根据我阅读的内容,我明白我不需要,就像之前使用不推荐的GCMBaseIntentService一样
    总结如下:
    • 我真的很想得到一些帮助来理解我需要什么来在我的设备上接收信息,因为实际上我不知道在哪里可以找到使用这个系统所需的信息
    谢谢

    PS:如果你需要我的代码的一部分,就问我

    编辑 我的
    清单

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.androidhive.pushnotifications2"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <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" />
    
        <permission
            android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.VIBRATE" />
    
        <!-- Main activity. -->
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
    
            <!-- Register Activity -->
            <activity
                android:name="com.androidhive.pushnotifications2.cop.RegisterActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <!-- Main Activity -->
            <activity
                android:name="com.androidhive.pushnotifications2.MainActivity"
                android:configChanges="orientation|keyboardHidden"
                android:label="@string/app_name" >
            </activity>
    
    
            <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.androidhive.pushnotifications2" />
                </intent-filter>
            </receiver>         
        </application>
    
    </manifest>
    

    你能发布你的Android清单吗?请发布你正在使用的
    GcmBroadcastReceiver
    类。
    package com.androidhive.pushnotifications2;
    
    import android.app.Activity;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    
    import com.google.android.gms.gcm.GoogleCloudMessaging;
    
    public class GcmBroadcastReceiver extends BroadcastReceiver {
        static final String TAG = "GCMDemo";
        public static final int NOTIFICATION_ID = 1;
        private NotificationManager mNotificationManager;
        NotificationCompat.Builder builder;
        Context ctx;
        @Override
        public void onReceive(Context context, Intent intent) {
            ///
            WakeLocker.acquire(context);
            ///
            System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
            ctx = context;
            String messageType = gcm.getMessageType(intent);
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + intent.getExtras().toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        intent.getExtras().toString());
            } else {
                sendNotification("Received: " + intent.getExtras().toString());
            }
            WakeLocker.release();
            setResultCode(Activity.RESULT_OK);
        }
    
        // Put the GCM message into a notification and post it.
        private void sendNotification(String msg) {
            mNotificationManager = (NotificationManager)
                    ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    
            PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                    new Intent(ctx, MainActivity.class), 0);
    
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.common_signin_btn_icon_focus_light)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(msg))
            .setContentText(msg);
    
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    }