即使服务器收到成功消息,也无法在我的设备上接收GCM消息

戈文德

我收到成功消息,作为对gcm对Web服务器(本地主机)的响应。但是设备未收到该消息。请帮忙。下面是我使用的代码。

主要活动

package vitpat.placement;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;    
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {

    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";

    TextView textUnderProgressBar;
    String SENDER_ID = "794097343372";
    final String TAG = "GCMDemo";
    TextView mDisplay;
    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    SharedPreferences prefs;
    Context context;
    String regid;

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

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        textUnderProgressBar = (TextView) findViewById(R.id.textView1);
        context = getApplicationContext();

        //shared preference to check if the application is being run for the first time and the user is yet to be verified
        SharedPreferences prefToVerifyUser = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
        Boolean isUserVerified = prefToVerifyUser.getBoolean("UserVerified", false);
        Log.e("main activity", isUserVerified.toString());
        if(isUserVerified == false) {
            Intent intent = new Intent(this,Check_User_Authentication.class);
            startActivityForResult(intent, 0);
        }
        else {
        //end of shared preference check

        ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar1);
        progress.setIndeterminate(true);
        progress.setVisibility(View.VISIBLE);

        // Check device for Play Services APK.
        if(CheckForPlayStoreApp()) {
            //code
            textUnderProgressBar.setText("Contacting Server..");
            gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);
            if (regid.isEmpty()) {
                registerInBackground();
            }
            else {
                textUnderProgressBar.setText(regid.toString());
                /*GcmBroadcastReceiver gcr;
                GcmBroadcastReceiver.startWakefulService(getApplicationContext(), getIntent());*/

            }
        } else {
            Log.i(TAG, "No valid Google Play Services APK found.");
        }
        }
    }

    /**
     * Check the device to make sure it has the Google Play Services APK. If
     * it doesn't, display a dialog that allows users to download the APK from
     * the Google Play Store or enable it in the device's system settings.
     */
    private boolean CheckForPlayStoreApp() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i("CheckForPlayStoreApp", "This device is not supported.");
                finish();
            }
            return true;
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // You need to do the Play Services APK check here too.
    @Override
    protected void onResume() {
        super.onResume();
        CheckForPlayStoreApp();
    }

    /**
     * Gets the current registration ID for application on GCM service.
     * <p>
     * If result is empty, the app needs to register.
     *
     * @return registration ID, or empty string if there is no existing
     *         registration ID.
     */
    private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing regID is not guaranteed to work with the new
        // app version.
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }
    /**
     * @return Application's {@code SharedPreferences}.
     */
    private SharedPreferences getGCMPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but
        // how you store the regID in your app is up to you.
        return getSharedPreferences(MainActivity.class.getSimpleName(),
                Context.MODE_PRIVATE);
    }

    /**
     * @return Application's version code from the {@code PackageManager}.
     */
    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);
        }
    }

    /**
     * Registers the application with GCM servers asynchronously.
     * <p>
     * Stores the registration ID and app versionCode in the application's
     * shared preferences.
     */
    private void registerInBackground() {
        Log.e("inside register", "sample0");
        new AsyncTask<Void, Void, Void>() {


            @Override
            protected Void doInBackground(Void... params) {

                Log.e("inside register", "sample1");
                  String msg = "";
                    try {
                        if (gcm == null) {
                            gcm = GoogleCloudMessaging.getInstance(context);
                        }
                        regid = gcm.register(SENDER_ID);
                        Log.e("inside register", regid);
                        msg = "Device registered, registration ID=" + regid;

                        // You should send the registration ID to your server over HTTP,
                        // so it can use GCM/HTTP or CCS to send messages to your app.
                        // The request to your server should be authenticated if your app
                        // is using accounts.
                        sendRegistrationIdToBackend();

                        // For this demo: we don't need to send it because the device
                        // will send upstream messages to a server that echo back the
                        // message using the 'from' address in the message.

                        // Persist the regID - no need to register again.
                        storeRegistrationId(context, regid);
                    } catch (IOException ex) {
                        msg = "Error :" + ex.getMessage();
                        // If there is an error, don't just keep trying to register.
                        // Require the user to click a button again, or perform
                        // exponential back-off.
                    }

                return null;
            }


        }.execute(null, null, null);

    }

    /**
     * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
     * or CCS to send messages to your app. Not needed for this demo since the
     * device sends upstream messages to a server that echoes back the message
     * using the 'from' address in the message.
     */
        private void sendRegistrationIdToBackend() {
        // Your implementation here.
        String url = "http://192.168.43.112/dist/addregistrationid.php";

        SharedPreferences prefUserDetails = getApplicationContext().getSharedPreferences("UserDetails", 0);
        String registerNumber = prefUserDetails.getString("registerNumber", null);


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("regid", regid));
        params.add(new BasicNameValuePair("regno", registerNumber));
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
        } 
        catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            HttpResponse httpResponse = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }         
    }

    /**
     * Stores the registration ID and app versionCode in the application's
     * {@code SharedPreferences}.
     *
     * @param context application's context.
     * @param regId registration ID
     */
    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }


}



GcmBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
            // Start the service, keeping the device awake while it is launching.
         startWakefulService(context, (intent.setComponent(comp)));
         setResultCode(Activity.RESULT_OK);


    }

}



GcmIntentService

public class GcmIntentService extends IntentService {

    Context context;
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        Bundle extras = intent.getExtras();
        String msg = intent.getStringExtra("message");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

         if (!extras.isEmpty()) {

             if (GoogleCloudMessaging.
                        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                    sendNotification("Send error: " + extras.toString());
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) {
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                // If it's a regular GCM message, do some work.
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    for (int i=0; i<5; i++) {
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                        }
                    }
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    //sendNotification("Received: " + extras.toString());
                    sendNotification(msg);
                    Log.i(TAG, "Received: " + extras.toString());
                }
            }
         GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
       // .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }
}

表现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vitpat.placement"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <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="vitpat.placement.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="vitpat.placement.permission.C2D_MESSAGE" />

    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="vitpat.placement.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>
        <activity 
            android:name="vitpat.placement.Check_User_Authentication"
            android:theme="@android:style/Theme.Dialog">
        </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="vitpat.placement" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmIntentService" />
        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
        <activity android:name="vitpat.placement.ReceiveActivity"></activity>
    </application>


</manifest>
戈文德

我自己找到了解决方案。我正在使用“ android api key ”将消息从Web服务器(php)发送到gcm服务器,这就是问题所在。使用“浏览器api ”键解决了该问题。从Google控制台创建一个新的“浏览器api密钥”,并在php代码中使用它,将消息从Web服务器发送到gcm。

如果要从android设备向gcm发送消息,请尝试使用android key。确保您在google控制台下的同一项目中创建了所有这些键。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

即使服务器收到成功消息,也无法在我的设备上接收GCM消息

来自分类Dev

GCM消息在服务器上成功发送,但在设备上未收到

来自分类Dev

GCM消息在服务器上成功发送,但在设备上未收到

来自分类Dev

为什么我无法在Android设备上接收来自GCM的消息

来自分类Dev

尽管所有消息均从服务器成功发送,但某些设备仍未收到GCM推送

来自分类Dev

发送通知时服务器未接收到设备未注册的消息

来自分类Dev

服务器无法在c winsock编程中接收到客户端消息

来自分类Dev

我如何使Jenkins服务器即使成功构建也发送自定义消息

来自分类Dev

无法使用pysockets从服务器接收消息

来自分类Dev

节点服务器无法从Socket.IO接收消息

来自分类Dev

Boost Client无法从服务器接收消息

来自分类Dev

无法从 Laravel 回显服务器接收广播消息

来自分类Dev

设备未收到GCM消息

来自分类Dev

我用netty写了一个应用程序,从客户端向服务器发送消息,但服务器无法接收消息

来自分类Dev

如何从流服务器接收消息?

来自分类Dev

服务器未正确接收消息?

来自分类Dev

服务器断开并收到LWT消息?

来自分类Dev

从应用服务器接收GCM推送通知消息的步骤

来自分类Dev

java中的XMPP服务器没有收到来自GCM服务器的所有消息

来自分类Dev

Express Node js Webssockets正在从Websocket服务器接收消息,但无法发送消息

来自分类Dev

无法从php接收到Ajax,jQuery的成功消息

来自分类Dev

GCM推送消息将一直保留在服务器上,直到收到新消息为止

来自分类Dev

GCM连接服务器如何将消息发送到Android设备?

来自分类Dev

客户端-服务器python:接收到的消息不能单独打印

来自分类Dev

套接字未同时从服务器接收到两个消息

来自分类Dev

为什么我的Android客户端无法从Winsock C ++服务器接收字符串消息?

来自分类Dev

即使邮件功能返回true,我的服务器也无法发送电子邮件

来自分类Dev

即使 apache 服务器正在运行,我也无法访问 phpmyadmin

来自分类Dev

datalayerlistener无法在可穿戴设备上接收消息(意图过滤器不起作用)

Related 相关文章

  1. 1

    即使服务器收到成功消息,也无法在我的设备上接收GCM消息

  2. 2

    GCM消息在服务器上成功发送,但在设备上未收到

  3. 3

    GCM消息在服务器上成功发送,但在设备上未收到

  4. 4

    为什么我无法在Android设备上接收来自GCM的消息

  5. 5

    尽管所有消息均从服务器成功发送,但某些设备仍未收到GCM推送

  6. 6

    发送通知时服务器未接收到设备未注册的消息

  7. 7

    服务器无法在c winsock编程中接收到客户端消息

  8. 8

    我如何使Jenkins服务器即使成功构建也发送自定义消息

  9. 9

    无法使用pysockets从服务器接收消息

  10. 10

    节点服务器无法从Socket.IO接收消息

  11. 11

    Boost Client无法从服务器接收消息

  12. 12

    无法从 Laravel 回显服务器接收广播消息

  13. 13

    设备未收到GCM消息

  14. 14

    我用netty写了一个应用程序,从客户端向服务器发送消息,但服务器无法接收消息

  15. 15

    如何从流服务器接收消息?

  16. 16

    服务器未正确接收消息?

  17. 17

    服务器断开并收到LWT消息?

  18. 18

    从应用服务器接收GCM推送通知消息的步骤

  19. 19

    java中的XMPP服务器没有收到来自GCM服务器的所有消息

  20. 20

    Express Node js Webssockets正在从Websocket服务器接收消息,但无法发送消息

  21. 21

    无法从php接收到Ajax,jQuery的成功消息

  22. 22

    GCM推送消息将一直保留在服务器上,直到收到新消息为止

  23. 23

    GCM连接服务器如何将消息发送到Android设备?

  24. 24

    客户端-服务器python:接收到的消息不能单独打印

  25. 25

    套接字未同时从服务器接收到两个消息

  26. 26

    为什么我的Android客户端无法从Winsock C ++服务器接收字符串消息?

  27. 27

    即使邮件功能返回true,我的服务器也无法发送电子邮件

  28. 28

    即使 apache 服务器正在运行,我也无法访问 phpmyadmin

  29. 29

    datalayerlistener无法在可穿戴设备上接收消息(意图过滤器不起作用)

热门标签

归档