簡體   English   中英

錯誤-使用Google Cloud Messaging(GCM)的Android Push Notification

[英]ERROR - Android Push Notification using Google Cloud Messaging (GCM)

我正在研究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):at 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):錯誤lo 添加/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):at java.util.zip.Inflater。(Inflater.java:82)10-22 20:55: 20.140:E / StrictMode(693):at java.util.zip.GZIPInputStream。(GZIPInputStream.java:96)10-22 20:55:20.140:E / StrictMode(693):at java.util.zip.GZIPInputStream。 (GZIPInputStream.java:81)10-22 20:55:20.140:E / StrictMode(693):at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)10-22 20:55 :20.140:E / StrictMode(693):at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)10-22 20:55:20.140:E / StrictMode(693):at com。 android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)10-22 20:55:20.140:E / StrictMode(693):at 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):在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.ja(SourceFile:220)10 -22 20:55:20.140:E / StrictMode(693):at 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):at 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):at 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)

從您的logcat ,它表明您需要執行以下操作:

在您的Android Manifest添加以下標記作為元素的子代:

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

您可以在AndroidManifest文件中的</receiver></application>之間添加行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM