【发布时间】:2015-10-22 02:52:02
【问题描述】:
我正在处理 android 推送通知并尝试获取注册 ID,以便我可以从我的 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): 错误加载 /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): 在 dalvik.system.CloseGuard.open(CloseGuard.java:184) 10-22 20:55:20.140: E/StrictMode(693): 在 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): 在 com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 10-22 20:55:20.140: E/StrictMode(693): 在 com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.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): at com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811) 10-22 20:55:20.140: E/StrictMode(693): at 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): at com.google.android.gms.auth.be.j.a(SourceFile:220) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.be.appcert.a.a(SourceFile:263) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.be.appcert.a.a(SourceFile:132) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.be.appcert.b.a(SourceFile:43) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.b.b.a(SourceFile:62) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.b.a.a(SourceFile:120) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.b.a.a(SourceFile:61) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile:44) 10-22 20:55:20.140: E/StrictMode(693): at com.google.android.gms.gcm.al.run(SourceFile:135)
【问题讨论】:
-
从您的 .Net 应用程序发送消息的请求格式是什么?
-
嗨,很抱歉回复晚了,我的 .net 在 c# 中
-
您是否正在创建将 GCM 消息发送到设备的请求?在任何情况下,您都没有使用推荐的在 Android 上使用 GCM 的方式,请查看此快速入门并将其用作您的应用程序的基础。 github.com/googlesamples/google-services/tree/master/android/…
-
谢谢!但是我已经从使用 GCM 切换到 PARSE 来发送推送通知,我可以将通知从解析服务器发送到我的 android 手机,我仍在尝试找出一种方法将通知从 asp.net c# 发送到我的 android 手机使用解析
标签: java android eclipse google-cloud-messaging