0

I followed the tutorial Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL. My application only gets the notifications if I synchronize my mobile with Gmail before installing the application. If I install the application first and then synchronize my mobile with Gmail, it's not geting the notifcations.

Code

public class MainActivity extends Activity {

    // Asynchronous task
    AsyncTask<Void, Void, Void> mRegisterTask;

    // Alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();

    // Connection detector
    ConnectionDetector cd;

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

        cd = new ConnectionDetector(getApplicationContext());

        // 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);

        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 {
            // The 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);
                        ServerUtilities.register(context,  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) {
            // 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
             * */

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

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.androidhive.pushnotifications"
          android:versionCode="1"
          android:versionName="1.0" >

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <!-- GCM connects to Internet Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Creates a custom permission so only this app can receive its messages. -->
    <permission
        android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission
       android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- Main activity. -->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!-- Register Activity -->
        <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>

        <!-- Main Activity -->
        <!--  <activity
                    android:name=".MainActivity"
                    android:configChanges="orientation|keyboardHidden"
                    android:label="@string/app_name" >
                </activity>
        -->
        <receiver
            android:name="com.google.android.gcm.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.pushnotifications" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />
    </application>
 </manifest>

LogCat error

10-01 02:35:12.809: V/GCMRegistrar(397): Unregistering receiver
10-01 02:35:12.809: E/UnRegister Receiver Error(397): > Receiver not registered:
com.google.android.gcm.GCMBroadcastReceiver@43e59e68

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2762662
  • 99
  • 1
  • 1
  • 10

1 Answers1

0

There must be at least one Google account registered on the device, else push notification does not work. You may refer to this document for more information. As it is mentioned,

For GCM to work, the mobile device must include at least one Google account if the device is running a version lower than Android 4.0.4.

EDIT : Also refer to this answer and make sure you have the same package name in your permission

<permission
    android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

and in the line below inside the intent-filter

<category android:name="com.androidhive.pushnotifications" />
Community
  • 1
  • 1
  • ok but in this code before sencronize account if run application logcat sy registered with GCM but when sencronize with gmail and run again application deice id not insert in ph server why??? – user2762662 Sep 30 '13 at 20:50
  • Which device are you using and which version of Android it is having? –  Sep 30 '13 at 20:52
  • if i first sencronize gmaila ccont with device and then install application on mobile regId inser in php satabase but if install application first then sencronize with gmail account applicationnot getting any notification also RegId not insert in php database where is problem on code please help – user2762662 Sep 30 '13 at 20:52
  • check my code above and sample website which i follow only tell me before sencronize deivice with gmail if aplication install and then sencronize with gmail mobile why regId not insert in database???? – user2762662 Sep 30 '13 at 20:54
  • it is possible in android to sent notification without sencronize device wth gmail account? i see this but is not give meserver code https://parse.com/tutorials/android-push-notifications – user2762662 Sep 30 '13 at 20:56
  • plz check my code above and tell me why my regId not insert in php database if i install application before sencronize iwth gmail account – user2762662 Sep 30 '13 at 20:58
  • Make sure you have all the permissions correctly put inside your manifest file. –  Sep 30 '13 at 21:00
  • yes ap is working perfectly if i regster with gmail account firt then install application but if i first install ap then sencronize with gmail my regId not insert in database of php server why??? plz check if startement above i think id first register with gcm if gmail acount not sencronize and next time only resieve notification from server and due to which device id not getting reponse form server bcoz regId not insert in Database may be please check above code problem is somewhere – user2762662 Sep 30 '13 at 21:05
  • code is working no issue im reciving notification from server only issue is if i install application after sencronizde mobile with gmaila ccount is work perfectly but if i install app before sencronize with gmail im not getting notificaion also notregId insert in Database – user2762662 Sep 30 '13 at 21:06
  • Can you paste your manifest file where you have specified the permissions and the receiver blocks. –  Sep 30 '13 at 21:12
  • i completely follow this tutorial http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ – user2762662 Sep 30 '13 at 21:13
  • yes i did this i have only problem sencronize deivce after install application not getting notification from server and also regId not nsert in database ofphp server but if sencronize deivice before install application i recive notification from server why?? – user2762662 Sep 30 '13 at 21:36