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

Posted

技术标签:

【中文标题】收到推送通知且未调用 CGM 意图服务时应用程序崩溃【英文标题】:App crash when receives push notification and CGM intent service not being called 【发布时间】:2015-05-12 16:48:36 【问题描述】:

一定有什么我做错了,但我是 android studio 的新手,我找不到它。我有一个应用程序,它显示四个 webViews,每个都在一个选项卡中,我正在尝试发送推送,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 意图服务时,这是我的活动主文件:

   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>

日志猫:

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

【问题讨论】:

抱歉,您所说的跟踪是什么意思?没有错误,它工作正常,你可以看到 json 说 gcm 消息发送正常。但是我的设备从未收到该消息,因此我的 GCM Intent 服务从未调用... 我的意思是在您的 gcm 服务/gcm 注册过程中添加一些日志消息,然后发布这些日志 添加了logcat,希望对您有帮助 所以只是为了确保注册顺利将 mDisplay 替换为 Visible TextView 的一些参考,以便您检查结果 【参考方案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 消息触发接收方,然后接收方触发服务。

【讨论】:

如何向接收器添加日志? 我已经修复了我的清单文件并在此处使用新的清单文件进行了编辑,但仍然没有调用 GCM 意图服务。 @user3033437 Log.d("Receiver", "onReceiver"); 在你的广播接收器的onReceiver 方法中......你的接收器被调用了吗? @user3033437 您之前的权限是正确的。对此感到抱歉...只有您的类别需要更正 非常感谢@LordRaydenMK 它就像你说的那样工作,将它移出 mainActivity。我真的很感激。【参考方案2】:

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

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());
                

【讨论】:

以上是关于收到推送通知且未调用 CGM 意图服务时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

具有相同意图的推送通知打开 Activity

收到推送通知

如何在快速接收推送通知时进行服务器调用?

当您收到带有 onesignal 的推送通知时自动打开应用程序

收到推送通知时如何更改范围变量

当应用程序在后台时,推送通知未调用 didReceiveRemoteNotification