使用firebase发送推送通知 - FATAL EXCEPTION(android)

时间:2017-04-23 13:13:06

标签: android firebase firebase-cloud-messaging firebase-notifications

首次尝试使用firebase推送通知工作并不顺利。我已按照列出的here教程进行设置,但每当我尝试发送通知时,应用程序都会无法正常关闭",并且逻辑会抛出以下崩溃信息:

logcat的:

--------- beginning of crash
04-23 23:04:28.081 7395-7395/com.surfdogdesigns.pandsi E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.surfdogdesigns.pandsi, PID: 7395
     java.lang.RuntimeException: Unable to instantiate service com.surfdogdesigns.pandsi.MainActivity$MyFirebaseMessagingService: java.lang.InstantiationException: class com.surfdogdesigns.pandsi.MainActivity$MyFirebaseMessagingService has no zero argument constructor
         at android.app.ActivityThread.handleCreateService(ActivityThread.java:2746)
         at android.app.ActivityThread.access$1800(ActivityThread.java:151)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:135)
         at android.app.ActivityThread.main(ActivityThread.java:5254)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
      Caused by: java.lang.InstantiationException: class com.surfdogdesigns.pandsi.MainActivity$MyFirebaseMessagingService has no zero argument constructor
         at java.lang.Class.newInstance(Class.java:1597)
         at android.app.ActivityThread.handleCreateService(ActivityThread.java:2743)
         at android.app.ActivityThread.access$1800(ActivityThread.java:151) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5254) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
      Caused by: java.lang.NoSuchMethodException: <init> []
         at java.lang.Class.getConstructor(Class.java:531)
         at java.lang.Class.getDeclaredConstructor(Class.java:510)
         at java.lang.Class.newInstance(Class.java:1595)
         at android.app.ActivityThread.handleCreateService(ActivityThread.java:2743) 
         at android.app.ActivityThread.access$1800(ActivityThread.java:151) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5254) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
04-23 23:04:28.082 1543-1559/system_process W/ActivityManager:   Force finishing activity 1 com.surfdogdesigns.pandsi/.MainActivity
04-23 23:04:28.093 1164-1164/? E/EGL_emulation: tid 1164: eglCreateSyncKHR(1299): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-23 23:04:28.211 1543-1599/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
04-23 23:04:28.225 1174-1724/? E/Drm: Failed to find drm plugin
04-23 23:04:28.226 2728-6161/com.google.android.gms.unstable W/DG.WV: Widevine DRM not supported on this device
                                                                      android.media.UnsupportedSchemeException: Failed to instantiate drm object.
                                                                          at android.media.MediaDrm.native_setup(Native Method)
                                                                          at android.media.MediaDrm.<init>(MediaDrm.java:180)
                                                                          at ono.a(:com.google.android.gms:122)
                                                                          at okh.run(:com.google.android.gms:1095)
                                                                          at lmq.run(:com.google.android.gms:450)
                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                          at lra.run(:com.google.android.gms:17)
                                                                          at java.lang.Thread.run(Thread.java:818)
04-23 23:04:28.661 1543-1564/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{cb0d51e u0 com.surfdogdesigns.pandsi/.MainActivity t31 f}
04-23 23:04:29.521 1828-2091/com.google.android.googlequicksearchbox W/OpenGLRenderer: Incorrectly called buildLayer on View: aep, destroying layer...
04-23 23:04:29.633 3034-6156/com.google.android.gms.persistent W/GLSUser: [AppCertManager] IOException while requesting key: 
                                                                          java.io.IOException: Invalid device key response.
                                                                              at ewg.a(:com.google.android.gms:274)
                                                                              at ewg.a(:com.google.android.gms:4238)
                                                                              at ewf.a(:com.google.android.gms:45)
                                                                              at evz.a(:com.google.android.gms:50)
                                                                              at evy.a(:com.google.android.gms:104)
                                                                              at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(:com.google.android.gms:4049)
                                                                              at edi.call(:com.google.android.gms:2041)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                              at lmq.run(:com.google.android.gms:450)
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                              at lra.run(:com.google.android.gms:17)
                                                                              at java.lang.Thread.run(Thread.java:818)
04-23 23:04:29.693 1646-1646/com.android.systemui W/ResourceType: No package identifier when getting value for resource number 0x00000000
04-23 23:04:29.694 1646-1646/com.android.systemui W/PackageManager: Failure retrieving resources for com.surfdogdesigns.pandsi: Resource ID #0x0
04-23 23:04:33.229 2285-7929/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.
04-23 23:04:34.372 2285-2299/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/metrics.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
04-23 23:04:34.380 2285-2299/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
04-23 23:04:34.388 2285-2299/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
04-23 23:04:39.325 1543-1564/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{cb0d51e u0 com.surfdogdesigns.pandsi/.MainActivity t31 f}
04-23 23:04:42.527 8148-8148/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
04-23 23:04:42.532 8148-8148/? D/AndroidRuntime: CheckJNI is ON
04-23 23:04:42.561 8148-8148/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-23 23:04:42.561 8148-8148/? E/android.os.Debug: failed to load memtrack module: -2
04-23 23:04:42.574 8148-8148/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
04-23 23:04:42.579 1543-1560/system_process I/ActivityManager: Force stopping com.surfdogdesigns.pandsi appid=10058 user=0: from pid 8148
04-23 23:04:42.579 1543-1560/system_process I/ActivityManager: Killing 7395:com.surfdogdesigns.pandsi/u0a58 (adj 0): stop com.surfdogdesigns.pandsi
04-23 23:04:42.580 1543-1560/system_process W/libprocessgroup: failed to open /acct/uid_10058/pid_7395/cgroup.procs: No such file or directory
04-23 23:04:42.588 1543-1560/system_process W/ActivityManager: Scheduling restart of crashed service com.surfdogdesigns.pandsi/.MainActivity$MyFirebaseMessagingService in 29020ms
04-23 23:04:42.588 1543-1560/system_process I/ActivityManager:   Force stopping service ServiceRecord{2a3bed30 u0 com.surfdogdesigns.pandsi/.MainActivity$MyFirebaseMessagingService}
04-23 23:04:42.599 1543-1811/system_process I/ActivityManager: Killing 2654:com.android.settings/1000 (adj 15): empty #17
04-23 23:04:42.599 1543-1811/system_process W/libprocessgroup: failed to open /acct/uid_1000/pid_2654/cgroup.procs: No such file or directory
04-23 23:04:42.600 8148-8148/? D/AndroidRuntime: Shutting down VM
04-23 23:04:42.608 1543-1560/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 7395 uid 10058
04-23 23:04:42.625 2004-8160/com.google.android.googlequicksearchbox:search I/HotwordRecognitionRnr: Starting hotword detection.
04-23 23:04:42.627 2004-8159/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_starting gzi@cc534c0
04-23 23:04:42.636 1174-1542/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
04-23 23:04:42.654 1174-8162/? I/AudioFlinger: AudioFlinger's thread 0xb5d99000 ready to run
04-23 23:04:42.663 2004-8159/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started gzi@cc534c0
04-23 23:04:42.670 2004-2004/com.google.android.googlequicksearchbox:search I/HotwordWorker: onReady
04-23 23:04:42.750 2285-8164/com.google.android.gms W/IcingInternalCorpora: getNumBytesRead when not calculated.
04-23 23:04:42.812 2285-2425/com.google.android.gms I/Icing: Usage reports 0 indexed 0 rejected 0 imm upload true

主要活动:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MainActivity extends AppCompatActivity {

    public MainActivity() {

    }
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        private static final String TAG = "FCM Service";
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            //
            //TODO: Handle FCM messages here.
            //If the application is in the foreground handle both data and notification messages here.
            //Also if you intend on generating your own notifications as a result of a received FCM
            //message, here is where that should be initiated.
            //
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        }
    }

    public class FirebaseIDService extends FirebaseInstanceIdService {
        private static final String TAG = "FirebaseIDService";

        @Override
        public void onTokenRefresh() {
            // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Refreshed token: " + refreshedToken);

            // TODO: Implement this method to send any registration to your app's servers.
            sendRegistrationToServer(refreshedToken);
        }

        /**
         * Persist token to third-party servers.
         *
         * Modify this method to associate the user's FCM InstanceID token with any server-side account
         * maintained by your application.
         *
         * @param token The new token.
         */
        private void sendRegistrationToServer(String token) {
            // Add custom implementation, as needed.
        }
    }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.surfdogdesigns.pandsi">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"


        >
        <activity android:name=".MainActivity">

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

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

        </activity>

        <service android:name=".MainActivity$MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service android:name=".MainActivity$FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>


    </application>



</manifest>

有人能看出出了什么问题吗?以及如何解决它?

3 个答案:

答案 0 :(得分:1)

您需要在类MyFirebaseMessagingService

中添加空构造函数
public MyFirebaseMessagingService() {
    super("MyFirebaseMessagingService");
}

有关说明,您可以查看详细信息here

答案 1 :(得分:1)

我还没有测试过,但我怀疑你的服务不能成为MainActivity的内部类。将示例声明为非嵌套类将更安全,可能是必需的,如示例项目中所做的那样:

MyFirebaseInstanceIDService

MyFirebaseMessagingService

如果您将服务声明为 static 嵌套类,那么服务也可能是MainActivity的嵌套类:

public static class MyFirebaseMessagingService...

答案 2 :(得分:0)

检查是否具有实现依赖的其他版本

implementation 'com.google.android.gms:play-services-gcm:+'
implementation 'com.google.firebase:firebase-core:+'
implementation 'com.google.firebase:firebase-messaging:+'