0

My app won't receive any messages from GCM, when generate number and send it by contact activity I receive it in mysql like this #######:###################aWvJRwO98DIipu8lhqrH7CWYSkHDqv_Cn4liO49HMizICLwZ

so I use whole the number in registration_ids, however I get error "NotRegistered" in response and I don't know where is the problem I tested also by testing sites

this page whose response to send message

  <?php

class GSM {

function _construct() {

}

    public function send_notification($registration_ids, $message){

        include_once './config.php';


        $url = 'https://gcm-http.googleapis.com/gcm/send';

        $fileds = array(
            'registration_ids' => $registration_ids,
            'message' => $message, );


        $headers = array ( 
              'Authorization: key=' . GOOGLE_API_KEY,
              'Content-Type: application/json'
            );
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fileds));

        $result = curl_exec($ch);
        if ($result === false) {

            die('Cutl failed:' . curl_error($ch));
        }
        curl_close($ch);
        echo $result;
    }
}



?>

getRegId in Contact class

public void getRegId() {
new AsyncTask<Void, Void, String>() {

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

        String msg = "";

        try {
            if (gcm == null) {
                InstanceID instanceID = InstanceID.getInstance(getActivity().getApplicationContext());
                regId = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE);

                msg = "Device registration, registration ID=" + regId;

                Log.i(TAG, msg);
            }

        } catch (IOException e) {
            e.printStackTrace();
            msg = "Error :" + e.getMessage();
        }
        return msg;
    }

}.execute(null, null, null);

}

}

class to receive the message

public class GCMIntentService extends GcmListenerService{


  private static final String TAG = "GCMIntentService";

@Override
public void onMessageReceived(String from, Bundle data) {

    String message = data.getString("message");
    Log.d(TAG, "from:" + from);
    Log.d(TAG, "message:" + message);

    sendNotification(message);
}
private void sendNotification(String message){
  Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("New Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }

}
}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
    android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" />

.........

<service
            android:name=".GCMIntentService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

I added also

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
    classpath 'com.google.gms:google-services:2.0.0-alpha3'

and

    apply plugin: 'com.android.application'

    .........

dependencies {
    compile "com.google.android.gms:play-services:8.4.0"
}

.........

apply plugin: 'com.google.gms.google-services'
ahmed
  • 11
  • 1
  • If your device is generating the registration id then you can check notification by sending it from android rest client like `postman` or `advance rest client` – Pankaj Jan 20 '16 at 06:20
  • Check [this link](http://stackoverflow.com/a/34870948/2715073) to send message from rest client using advance rest client – Pankaj Jan 20 '16 at 06:22
  • I did but won't to receive any message, my device is virtual by genymotion – ahmed Jan 20 '16 at 06:46
  • does your genymotion simulator have Google services installed? – gherkin Jan 21 '16 at 11:14
  • I use it in my phone and I get success on site but my phone does not receive anything until now – ahmed Jan 25 '16 at 17:44

2 Answers2

0

Check Your Browser Key which is you generate is correct in google api developer console and also in your .php file in which you pass browser-key and UDID.

You can test it online by http://www.androidbegin.com/tutorial/gcm.html

  • In this Google API Key (with IP locking) is your : browser key which you did in google developer console and Device Registration ID : is generate on android device and message is your push message. –  Jan 20 '16 at 11:17
  • Oh sorry I try it and I got {"multicast_id":62114168555656533,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]} – ahmed Jan 20 '16 at 11:37
  • check again your browser key and is this perfect ? –  Jan 20 '16 at 12:16
  • I think something wrong with app codes but I don't know where because I get same message all times – ahmed Jan 20 '16 at 19:20
0

Make sure that your push server remove stored registration id if GCM response 'notRegistered' error. Server will continue get this error when trying to send message with stored regId.

You may want to check on a similar issue related to 'notRegistered'

You may refer to this link for more information about the error you are receiving: (https://developers.google.com/cloud-messaging/http-server-ref#error-codes)

Community
  • 1
  • 1
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30