Thứ Sáu, 20 tháng 2, 2015

[A Dev Time Diary] Google Cloud Messaging GCM for Android and Push Notification Tutorial by JavaPapers

First off, you can find the tutorial here:
http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/

All credit for Joe, great job :).

The thing is I used Joe's post as a guide to take my first steps with GCM, but the source code provided just didn't work at first. I have been searching all across the internet looking for solution but almost no clue. Now I get it, hope it helps :)

1. When you use the server (PHP or Java), you might run into this:
{“multicast_id”:5760807111590163335,”success”:0,”failure”:1,”canonical_ids”:0,”results”:[{“error”:”MismatchSenderId”}]}

There are several workaround of this error:
- The GOOGLE_API_KEY (in your server-side code) or the GOOGLE_PROJECT_ID (in your Config.java of the client code) is incorrect, or they are not from the same Google Cloud Project. Note that the GOOGLE_PROJECT_ID is the Project Number: on Google Developers Page, not the Project ID (I know, right?).
- In my case, and I don't know why, after making sure both the GOOGLE_API_KEY and the GOOGLE_PROJECT_ID are correct and still running into the same problem, I created a new Google Cloud Project, obtained the new keys and Id. Problem solved! If anyone has a clue about this, please let me know :).

2. You can successfully send a message from the server, but the app is forced closed everytime a notification comes. The logcat says the Receiver cannot be instantiated. Check if your AndroidManifest.xml has this:
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"
            />

3. Your message content is returned null. In your PHP, check this line:
$message = array("m" => $pushMessage);
It should be "message" instead of "m". No biggie, just correct the name of the field in the JSON.

If you have any other problems/ workaround/ explaination, please share in the comment down below.

Thanks and best regards :)

Không có nhận xét nào:

Đăng nhận xét