收到推送通知和未调用 CGM 意图服务时应用程序崩溃

App crash when receives push notification and CGM intent service not being called

一定是我做错了什么,但我是 android 工作室的新手,我找不到它。我有一个显示四个 webView 的应用程序,每个都在一个选项卡中,我正在尝试发送推送,json 响应代码没问题,但我在我的 GCM class 中建立了一些断点并且看起来它从未被调用过,并且我的应用程序在推送时崩溃 "arrives" 但没有显示任何推送通知。

json 响应代码:

{"multicast_id":4710708383941694704,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1431447972667924%e17d0a39f9fd7ecd"}]}{"registration_ids":["XxxxXXx"],"data":{"message":"New content available!"}}

这是我的 activity 主文件,当我有我的 GCM intent 服务时:

   public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Explicitly specify that GcmIntentService will handle the intent.
            ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());
            // Start the service, keeping the device awake while it is launching.
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
        }
    }


    public class GcmIntentService extends IntentService {
        public static final int NOTIFICATION_ID = 1;
        private NotificationManager mNotificationManager;
        NotificationCompat.Builder builder;

        public GcmIntentService() {
            super("GcmIntentService");

        }

        @Override
        protected void onHandleIntent(Intent intent) {
            Bundle extras = intent.getExtras();
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            // The getMessageType() intent parameter must be the intent you received
            // in your BroadcastReceiver.
            String messageType = gcm.getMessageType(intent);

            if (!extras.isEmpty()) {

                if (GoogleCloudMessaging.
                        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                    sendNotification("Send error: " + extras.toString());
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) {
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                    // If it's a regular GCM message, do some work.
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    for (int i=0; i<5; i++) {
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                        }
                    }
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    sendNotification("Received: " + extras.toString());
                    Log.i(TAG, "Received: " + extras.toString());
                }
            }
            // Release the wake lock provided by the WakefulBroadcastReceiver.
            GcmBroadcastReceiver.completeWakefulIntent(intent);
        }

        // Put the message into a notification and post it.
        // This is just one simple example of what you might choose to do with
        // a GCM message.
        private void sendNotification(String msg) {
            mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, MainActivity.class), 0);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_logo_guias)
                            .setContentTitle("GCM Notification")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(msg))
                            .setContentText(msg);

            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    }

Android 清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alfredo.guiaswebtabapp" >
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="22"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


    <permission android:name="com.example.alfredo.guiaswebtabapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.alfredo.guiaswebtabapp.permission.C2D_MESSAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MainActivity$GcmBroadcastReceiver"
            android:permission="com.example.alfredo.guiaswebtabapp.permission.C2D_MESSAGE">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.alfredo.guiaswebtabapp" />
            </intent-filter>
        </receiver>

        <service android:name=".MainActivity$GcmIntentService"
            android:enabled="true" />

        <activity
            android:name=".inicioActivity"
            android:label="@string/title_activity_inicio" >
        </activity>
        <activity
            android:name=".ActividadesActivity"
            android:label="@string/title_activity_actividades" >
        </activity>
    </application>

</manifest>

logcat:

05-13 10:50:19.329  17900-17900/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
05-13 10:50:19.329  17900-17900/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
05-13 10:50:31.209  17900-17900/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate receiver com.example.alfredo.guiaswebtabapp.MainActivity$GcmBroadcastReceiver: java.lang.InstantiationException: com.example.alfredo.guiaswebtabapp.MainActivity$GcmBroadcastReceiver
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:1777)
            at android.app.ActivityThread.access00(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.InstantiationException: com.example.alfredo.guiaswebtabapp.MainActivity$GcmBroadcastReceiver
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1409)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:1768)
            at android.app.ActivityThread.access00(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
            at dalvik.system.NativeStart.main(Native Method)
05-13 10:50:31.419      185-201/? E/﹕ Dumpstate > /data/log/dumpstate_app_error
05-13 10:50:35.179      274-274/? E/Launcher﹕ setWindowOpaque()
05-13 10:50:35.269      274-274/? E/Launcher﹕ MTP-LAUNCHER: media scanning not yet finished.
05-13 10:50:38.559      274-274/? E/Launcher﹕ setWindowOpaque()
05-13 10:52:38.579      185-215/? E/PowerManagerService﹕ CurLock p:3 mPS:1

您的 GcmBroadcastReceiver 有问题。权限和类别错误。清单中的接收方条目应如下所示:

   <receiver
        android:name=".MainActivity$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.alfredo.guiaswebtabapp" />
        </intent-filter>
    </receiver>

同时向接收器添加一个日志以检查它是否被调用。 GCM 消息触发接收方,然后接收方触发服务。

我有同样的错误,我通过在 public class GcmIntentService extends IntentService 内的 extras 添加一个过滤器来修复它,如下所示:

if(extras.getString("from").toString().equals(YOUR_SENDERID)){
                    sendNotification("Received: " + extras.toString(),extras);
                    Log.i("not an error received "+TAG, "Received: " + extras.toString());
                }
                else{
                    Log.e("error received "+TAG, "Received: " + extras.toString());
                }