ОШИБКА - Push-уведомление Android с использованием Google Cloud Messaging (GCM)
Я работаю над push-уведомлением Android и пытаюсь получить регистрационный идентификатор, чтобы я мог отправить push-уведомление со своей страницы администрирования asp.net на мое устройство Android, но мое приложение зависало всякий раз, когда я пытался запустить приложение, пожалуйста, помогите мне.
Мой файл XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gcmdemo.MainActivity" >
<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Мой файл Java
package com.example.gcmdemo;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
private final static String TAG = "GCMDEMO";
//My sender ID
protected String SENDER_ID = "10xxxxxxxxx53";
TextView mDisplay;
GoogleCloudMessaging gcm;
AtomicInteger msgId = new AtomicInteger();
SharedPreferences prefs;
Context context;
String regid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDisplay = (TextView) findViewById(R.id.display);
context = getApplicationContext();
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty())
{
registerInBackground();
}
else
{
Log.d(TAG, "No valid Google Play Services APK found.");
}
}
}
private void registerInBackground() {
new AsyncTask() {
protected Object doInBackground(Object... params)
{
String msg = "";
try
{
if (gcm == null)
{
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID); Log.d(TAG, "########################################");
Log.d(TAG, "Current Device's Registration ID is: "+msg);
}
catch (IOException ex)
{
msg = "Error :" + ex.getMessage();
}
return null;
} protected void onPostExecute(Object result)
{ //to do here };
}}.execute(null, null, null);
}
private String getRegistrationId(Context context) {
// TODO Auto-generated method stub
final SharedPreferences prefs = getGCMPreferences(context);
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
private int getAppVersion(Context context) {
// TODO Auto-generated method stub
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
private SharedPreferences getGCMPreferences(Context context) {
// TODO Auto-generated method stub
return getSharedPreferences(MainActivity.class.getSimpleName(),
Context.MODE_PRIVATE);
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.d(TAG, "This device is not supported - Google Play Services.");
finish();
}
return false;
}
return true;
}
protected void onResume()
{
super.onResume();
checkPlayServices();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Мой Android Манифест
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="com.example.gcmdemo.permission.C2D_MESSAGE"/>
<permission
android:name="com.example.gcmdemo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<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>
<service android:name=".GcmIntentService" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<actionandroid:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcmdemo" />
</intent-filter>
</receiver>
</application>
ЛОГ МОЙ ОШИБКИ
10-22 20:55:05.360: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
10-22 20:55:05.360: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
10-22 20:55:05.370: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
10-22 20:55:05.370: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
10-22 20:55:05.480: E/SoundPool(389): error loading /system/media/audio/ui/Effect_Tick.ogg
10-22 20:55:05.500: E/SoundPool(389): error loading /system/media/audio/ui/KeypressStandard.ogg
10-22 20:55:05.520: E/SoundPool(389): error loading /system/media/audio/ui/KeypressSpacebar.ogg
10-22 20:55:05.530: E/SoundPool(389): error loading /system/media/audio/ui/KeypressDelete.ogg
10-22 20:55:05.530: E/SoundPool(389): error loading /system/media/audio/ui/KeypressReturn.ogg
10-22 20:55:05.530: E/SoundPool(389): error loading /system/media/audio/ui/KeypressInvalid.ogg
10-22 20:55:05.680: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:05.690: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:05.690: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:05.690: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:05.690: E/SurfaceFlinger(54): glCheckFramebufferStatusOES error 760856444
10-22 20:55:05.700: E/SurfaceFlinger(54): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
10-22 20:55:05.700: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:05.700: E/libEGL(54): called unimplemented OpenGL ES API
10-22 20:55:07.080: E/AndroidRuntime(1311): FATAL EXCEPTION: main
10-22 20:55:07.080: E/AndroidRuntime(1311): Process: com.example.gcmdemo, PID: 1311
10-22 20:55:07.080: E/AndroidRuntime(1311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gcmdemo/com.example.gcmdemo.MainActivity}: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.os.Looper.loop(Looper.java:136)
10-22 20:55:07.080: E/AndroidRuntime(1311): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-22 20:55:07.080: E/AndroidRuntime(1311): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:55:07.080: E/AndroidRuntime(1311): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-22 20:55:07.080: E/AndroidRuntime(1311): at dalvik.system.NativeStart.main(Native Method)
10-22 20:55:07.080: E/AndroidRuntime(1311): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.google.android.gms.common.GooglePlayServicesUtil.zzad(Unknown Source)
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.example.gcmdemo.MainActivity.checkPlayServices(MainActivity.java:118)
10-22 20:55:07.080: E/AndroidRuntime(1311): at com.example.gcmdemo.MainActivity.onCreate(MainActivity.java:44)
10-22 20:55:07.080: E/AndroidRuntime(1311): на android.app.Activity.performCreate(Activity.java:5231) 10-22 20:55:07.080: E/AndroidRuntime(1311): на Android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 10-22 20:55:07.080: E/AndroidRuntime(1311): на android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 10-22 20:55:07.080: E/AndroidRuntime(1311): ... еще 11 10-22 20:55:09.490: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/Effect_Tick.ogg 10-22 20:55:09.490: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/Effect_Tick.ogg 10-22 20:55:09.500: E/SoundPool(389): ошибка загрузки / system / media / audio / ui /Effect_Tick.ogg 10-22 20: 55: 09.500: E / SoundPool (389): ошибка загрузки /system/media/audio/ui/Effect_Tick.ogg 10-22 20:55:09.530: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/Effect_Tick.ogg 10-22 20:55:09.530: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/KeypressStandard.ogg 10-22 20:55:09.540: E/SoundPool(389): ошибка loading /system/media/audio/ui/KeypressSpacebar.ogg 10-22 20:55:09.540: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/KeypressDelete.ogg 10-22 20:55:09.540: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/KeypressReturn.ogg 10-22 20:55:09.540: E/SoundPool(389): ошибка загрузки /system/media/audio/ui/KeypressInvalid.ogg 10-22 20:55:19.560: E/Drm(57): не удалось открыть каталог плагинов /vendor/lib/mediadrm 10-22 20:55:20.140: E/StrictMode(693): ресурс получен на трассировке прикрепленного стека, но никогда не выпускается. См. Java.io.Closeable для информации о том, как избежать утечек ресурсов. 10-22 20:55:20.140: E/StrictMode(693): java.lang.Throwable: явный метод завершения 'end' не вызывается 10-22 20:55:20.140: E/StrictMode(693): at dalvik.system.CloseGuard.open(CloseGuard.java:184) 10-22 20:55:20.140: E/StrictMode(693): at java.util.zip.Inflater.(Inflater.java:82) 10-22 20:55:20.140: E/StrictMode(693): в java.util.zip.GZIPInputStream.(GZIPInputStream.java:96) 10-22 20:55:20.140: E/StrictMode(693): в java.util.zip.GZIPInputStream.(GZIPInputStream.java:81) 10-22 20:55:20.140: E/StrictMode(693): на com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468) 10-22 20:55:20.140: E/StrictMode(693): на com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666) 10-22 20:55:20.140: E/StrictMode(693): на ком.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 10-22 20:55:20.140: E/StrictMode(693): в com.android.okhttp.internal.http.Httpurl.java: 296) 10-22 20: 55: 20.140: E / StrictMode (693): на com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503) 10-22 20: 55: 20.140: E / StrictMode (693): на com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136) 10-22 20: 55: 20.140: E / StrictMode (693): на com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.http.GoogleHttpClient.a(SourceFile:776) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:676) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:660) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.be.ja(SourceFile:220) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.be.appcert.aa(SourceFile:263) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.be.appcert.aa(SourceFile:132) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.be.appcert.ba(SourceFile:43) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.bba(SourceFile:62) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms. auth.baa(SourceFile:120) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.baa(SourceFile:61) 10-22 20:55:20.140: E/StrictMode(693): на com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile:44) 10-22 20:55:20.140: E/StrictMode(693): на com.google..android.gms.gcm.al.run(SourceFile:135)
1 ответ
От твоего logcat
, это показывает, что вам нужно сделать следующее:
В вашем Android Manifest
добавьте следующий тег в качестве дочернего элемента:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Вы можете добавить строки между </receiver>
а также </application>
в вашем AndroidManifest
файл.