Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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/1/visual-studio-2008/2.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中的GCM(谷歌云消息)推送通知_Android_Push Notification_Google Cloud Messaging - Fatal编程技术网

android中的GCM(谷歌云消息)推送通知

android中的GCM(谷歌云消息)推送通知,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在尝试使用GCM推送通知。我的设备已注册到服务器,并且我还创建了设备注册id。但是,当我试图从服务器发送消息时,该消息不会发送到已注册的设备。有人能帮我吗 我的主要活动 包com.ati.gcm 导入com.google.android.gcm.gcmregistar 导入android.os.AsyncTask 导入android.os.Bundle 导入android.app.Activity 导入android.content.BroadcastReceiver 导入android.c

我正在尝试使用GCM推送通知。我的设备已注册到服务器,并且我还创建了设备注册id。但是,当我试图从服务器发送消息时,该消息不会发送到已注册的设备。有人能帮我吗

我的主要活动

包com.ati.gcm

导入com.google.android.gcm.gcmregistar

导入android.os.AsyncTask

导入android.os.Bundle

导入android.app.Activity

导入android.content.BroadcastReceiver

导入android.content.Context

导入android.content.Intent

导入android.content.IntentFilter

导入android.util.Log

导入android.view.Menu

导入android.widget.TextView

导入android.widget.Toast

导入静态com.ati.gcm.CommonUtilities.DISPLAY\u MESSAGE\u ACTION

导入静态com.ati.gcm.CommonUtilities.EXTRA_消息

导入静态com.ati.gcm.CommonUtilities.SENDER\u ID

公共类MainActivity扩展了活动{

// label to display gcm messages
TextView lblMessage;
 
// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;
 
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
 
// Connection detector
ConnectionDetector cd;
 
public static String name;
public static String email;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     
    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(MainActivity.this,
                "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }
     
    // Getting name, email from intent
    Intent i = getIntent();
     
    name = i.getStringExtra("name");
    email = i.getStringExtra("email");     
     
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);

    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(this);

    lblMessage = (TextView) findViewById(R.id.lblMessage);
     
    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            DISPLAY_MESSAGE_ACTION));
     
    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(this);

    // Check if regid already presents
    if (regId.equals("")) {
        // Registration is not present, register now with GCM          
        GCMRegistrar.register(this, SENDER_ID);
    } else {
        // Device is already registered on GCM
        if (GCMRegistrar.isRegisteredOnServer(this)) {
            // Skips registration.             
            Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.
            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    // Register on our server
                    // On server creates a new user
                    ServerUtilities.register(context, name, email, regId);
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    mRegisterTask = null;
                }

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

/**
 * Receiving push messages
 * */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());
         
        /**
         * Take appropriate action on this message
         * depending upon your app requirement
         * For now i am just displaying it on the screen
         * */
         
        // Showing received message
        lblMessage.append(newMessage + "\n");          
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
         
        // Releasing wake lock
        WakeLocker.release();
    }
};
 
@Override
protected void onDestroy() {
    if (mRegisterTask != null) {
        mRegisterTask.cancel(true);
    }
    try {
        unregisterReceiver(mHandleMessageReceiver);
        GCMRegistrar.onDestroy(this);
    } catch (Exception e) {
        Log.e("UnRegister Receiver Error", "> " + e.getMessage());
    }
    super.onDestroy();
}
}

您是否将客户端的GCM id发送到您的服务器,服务器通过该id和GCM服务器与客户端通信。 您是否已将GCM权限插入您的舱单? GCMinentService.java是否位于项目的主包中?
广播接收器的邮政编码?检查API密钥。向设备发送消息时是否出现未经授权的错误?也为你的接收者显示代码。你的接收者发布代码。查看并演示如何实现GCMi。我检查了你的所有建议,但无法工作。谢谢,谢谢。是的,我的API密钥错了。我正在创建一个新项目,并使用新的API密钥,它运行良好。