Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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
Java Can';t使用GCM向android发送推送_Java_Android_Google Cloud Messaging - Fatal编程技术网

Java Can';t使用GCM向android发送推送

Java Can';t使用GCM向android发送推送,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,对不起我的英语。我已经花了2天时间,但我无法向android发送推送消息。我使用谷歌云信息。例如,在gcm中,我创建了一个新项目,并且我有一个id: 然后我启用了gcm并添加了服务器密钥 我有这样的代码: 清单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.

对不起我的英语。我已经花了2天时间,但我无法向android发送推送消息。我使用谷歌云信息。例如,在gcm中,我创建了一个新项目,并且我有一个id:

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

我有这样的代码:

清单

<?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>
GcmMessageHandler

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);
    }
}
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);
    }
Main

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();
    }

}
public类MainActivity扩展活动实现OnClickListener{
按钮btnRegId,取消注册;
编辑文本etRegId;
谷歌云通讯gcm;
字符串寄存器;
字符串项目_NUMBER=“308****”;
专用广播接收机注册广播接收机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unregister=(按钮)findviewbyd(R.id.unregister);
btnRegId=(按钮)findViewById(R.id.btnGetRegId);
etRegId=(EditText)findViewById(R.id.etRegId);
btnRegId.setOnClickListener(此);
}
public void getRegId(){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
字符串msg=“”;
试一试{
InstanceID InstanceID=InstanceID.getInstance(getApplicationContext());
字符串标记=instanceID.getToken(项目编号,
GoogleCloudMessaging.INSTANCE_ID_SCOPE,null);
msg=令牌;
//apiKey=msg;
GcmPubSub.getInstance(getApplicationContext()).subscribe(令牌“/topics/users”,null);
}捕获(IOEX异常){
msg=“错误:”+ex.getMessage();
}
返回味精;
}
@凌驾
受保护的void onPostExecute(字符串msg){
etRegId.setText(msg+“\n”);
Log.e(“key”,msg);
}
}.执行(空,空,空);
}
@凌驾
公共void onClick(视图v){
getRegId();
}
}

我使用并尝试向设备发送一些推送消息。服务就是成功。但是推入式android不会出现(我在日志中没有输出)

C2D_消息的包名是错误的,请将它们更改为您的包名。另外,请确保配置文件位于应用程序目录的build.gradle附近。编辑:浏览所有清单,并在看到hmk的地方更改包名。根据清单,您的程序包名称应为com.example.alexy.gcmclient。

我假设您的应用程序包名称必须为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>