Im currently making an android app based on gcm.When i run on real device,it has not shown any error and i can send messages(GAE logs also shows /send statement).The problem is another device didnt receive any messages.I refer to GAE logs and it doest show any /register statement.When i run on emulator,i will get this error.
04-12 14:56:38.897: E/AndroidRuntime(1181): java.lang.RuntimeException: An error occured while executing doInBackground()
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.lang.Thread.run(Thread.java:841)
04-12 14:56:38.897: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.example.gcm.GcmUtil$1.doInBackground(GcmUtil.java:142)
04-12 14:56:38.897: E/AndroidRuntime(1181): at com.example.gcm.GcmUtil$1.doInBackground(GcmUtil.java:1)
04-12 14:56:38.897: E/AndroidRuntime(1181): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-12 14:56:38.897: E/AndroidRuntime(1181): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-12 14:56:38.897: E/AndroidRuntime(1181): ... 4 more
The code is :
private void registerBackground() {
registrationTask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
//Log.d(TAG, "Attempt #" + i + " to register");
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(ctx);
}
String regid = gcm.register(Common.getSenderId());
ServerUtilities.register(Common.getPreferredEmail(),
regid);
// Save the regid - no need to register again.
setRegistrationId(regid);
return Boolean.TRUE;
} catch (IOException ex) {
//Log.e(TAG, "Failed to register on attempt " + i + ":"
+ ex);
if (i == MAX_ATTEMPTS) {
break;
}
try {
//Log.d(TAG, "Sleeping for " + backoff + " ms before
retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
//Log.d(TAG, "Thread interrupted: abort remaining
retries!");
Thread.currentThread().interrupt();
}
// increase backoff exponentially
backoff *= 2;
}
}
return Boolean.FALSE;
}
Note: I noticed a horizontal line on register at gcm.register...When i hover to it,its says @RequiresPermission(value="com.google.android.c2dm.permission.RECEIVE") @Deprecated
I already mentioned the permission in android manifest but still the line didnt gone.
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.heylo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<permission
android:name="com.example.heylo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.heylo.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<application
android:name="com.example.heylo.Common"
android:allowBackup="true"
android:icon="@drawable/ic_splash"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity android:name=".SplashScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.heylo" />
</intent-filter>
</receiver>
<provider
android:name="com.example.heylo.DataProvider"
android:authorities="com.example.heylo.provider"
android:exported="false" >
</provider>
<activity
android:name=".Message_List"
android:label="@string/title_activity_message__list" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>
</manifest>
The tutorial you follow is too old. Check message from google developers for your way:
GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens.
So its recommend to follow this guide from google developers: Guide
Regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.