java - 收到推送通知和未调用 CGM Intent 服务时应用程序崩溃

标签 java android android-intent push-notification google-cloud-messaging

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

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!"}}

这是我拥有 GCM Intent 服务时的 Activity 主文件:

   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 list 文件:

<?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>

日志:

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.access$2400(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.access$2400(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 有问题。权限和类别错误。 list 中的接收方条目应如下所示:

   <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 消息触发接收方,然后接收方触发服务。

关于java - 收到推送通知和未调用 CGM Intent 服务时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196963/

相关文章:

Java 编译时检查异常

java - 使用 ListIterator 从列表末尾开始连接

java - 在 Android Studio 中的 OnClick 类之间共享数组

java - 所有带有 Intent 的按钮都不起作用

android - 尝试使用 Intent 在android中打开特定文件夹

java - GWT 中的 "Development mode is loading..."和 "Waiting for launch URLs"

java - 在hadoop FS中写一个二叉树

android - 在自定义 lint 规则中解析资源值

Android Studio 无法仅在 Debug模式下查看异常日志

android - 如何在不重复的情况下更改 MaterialButton 的样式?