Ralatouf
Ralatouf

Reputation: 11

Square notification android (with GCM)

I try to put the GCM in place in my Android's app, I finaly succed to receive a "notification" but it's not the good one, just a square like it.

enter image description here

I don't know if the problem is on my side (android part) or on the other side (php)

On my side, the only problem can be on the manifest I think :

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

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="14" />

<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="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

<permission android:name="com.example.googlecloudmessaging8.permission.C2D_MESSAGE"
    android:protectionLevel="signature" ></permission>

<uses-permission android:name="com.example.googlecloudmessaging8.permission.C2D_MESSAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/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=".GcmReceiver"
        android:exported="true"
        android:process=":remote"
        android:permission="com.google.android.c2dm.permission.SEND">

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

            <category android:name="com.example.googlecloudmessaging8" />
        </intent-filter>
    </receiver>

    <service android:name=".GcmMessageHandler"></service>

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

GCMReceiver :

public class GcmReceiver extends WakefulBroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

GcmMessageHandler :

public class GcmMessageHandler extends IntentService{

String mes;
private Handler handler;

public GcmMessageHandler(){
    super("GcmMessageHandler");
}


@Override
public void onCreate() {
    super.onCreate();
    handler = new Handler();
}

@Override
protected void onHandleIntent(Intent intent){
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    mes = extras.getString("title");
    showToast();

    Log.i("GCM", "Received : (" + messageType + ") "  +extras.getString("title"));

    GcmReceiver.completeWakefulIntent(intent);

}

public void showToast(){
    handler.post(new Runnable(){

        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG).show();
        }
    });
}

}

On the gcm_test.php, I see this :

$aPushs[$iCpt]['message']       = 'message par defaut';
$aPushs[$iCpt]['link']          = 'http://www.google.fr/';    

And I only got a black square and not "message par defaut", someone knows why ? (on an other device it's a rectangle, empty too)

Upvotes: 1

Views: 186

Answers (1)

bjiang
bjiang

Reputation: 6078

I think your code is similar with this: https://www.youtube.com/watch?v=Vd8bIkE29U4

Please follow that step by step. I have tried, and it works perfectly.

Also, you can get source code refer to my github here:

https://github.com/jbj88817/Google-Cloud-Messaging-GCM-ex

Upvotes: 1

Related Questions