java - 火力地堡 : App crash when notification comes with app not opened

标签 java android

我遵循本教程 Lapit Chat App - Push Notifications - Firebase Tutorials - Part 24

一切顺利,只是当通知到来时我发现了一点问题,我的设备没有运行应用程序(后台模式),当我点击通知面板时,我的应用程序崩溃了。

如果我的设备之前打开过该应用程序,则不会出现该错误。当我点击通知面板时,应用程序正常运行并打开我的 ProfileActivity

我得到这个错误的 ErrorLog

07-29 16:36:54.512 16573-16573/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.bertho.chat, PID: 16573
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bertho.chat/com.bertho.chat.ProfileActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:234)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                    Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
                                                       at com.google.firebase.database.DatabaseReference.child(Unknown Source)
                                                       at com.bertho.chat.ProfileActivity.onCreate(ProfileActivity.java:56)
                                                       at android.app.Activity.performCreate(Activity.java:6285)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                       at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:234) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 

这是我的 ProfileActivity.java

public class ProfileActivity extends BaseActivity {

    private ImageView mProfileImage;
    private TextView mProfileName, mProfileStatus, mProfileFriendsCount;
    private Toolbar mToolbar;
    private Button mProfileSendReqBtn, mProfileDeclineReqBtn;
    private ProgressDialog mDialog;

    private FirebaseUser mCuurentUser;

    private String mCuurent_state;

    private DatabaseReference mUserDatabase, mFriendsReqDatabase, mFriendsDatabase, mNotificationDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_profile );

        final String user_id = getIntent().getStringExtra( "user_id" );

        mDialog = new ProgressDialog( this );

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child( "Users" ).child( user_id );
        mUserDatabase.keepSynced( true );

        mFriendsReqDatabase = FirebaseDatabase.getInstance().getReference().child( "FriendsRequest" );
        mFriendsReqDatabase.keepSynced( true );

        mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child( "FriendsData" );
        mFriendsDatabase.keepSynced( true );

        mNotificationDatabase = FirebaseDatabase.getInstance().getReference().child( "Notifications" );

        mProfileImage = (ImageView) findViewById( R.id.profile_image );
        mProfileName = (TextView) findViewById( R.id.profile_displayName );
        mProfileStatus = (TextView) findViewById( R.id.profile_status );
        mProfileFriendsCount = (TextView) findViewById( R.id.profile_totalFriends );
        mProfileSendReqBtn = (Button) findViewById( R.id.profile_send_fr_btn );
        mProfileDeclineReqBtn = (Button) findViewById( R.id.profile_decline_req_btn );

        mCuurentUser = FirebaseAuth.getInstance().getCurrentUser();

        showLoading( "Load Profile Data" );

        mCuurent_state = "not_friends";

        mUserDatabase.addValueEventListener( new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String display_name = dataSnapshot.child( "name" ).getValue().toString();
                String display_status = dataSnapshot.child( "status" ).getValue().toString();
                final String display_image = dataSnapshot.child( "image" ).getValue().toString();

                mProfileName.setText( display_name );
                mProfileStatus.setText( display_status );

                if (mCuurent_state == "not_friends") {
                    mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                    mProfileDeclineReqBtn.setEnabled( false );
                } else if (mCuurent_state == "received") {
                    mProfileDeclineReqBtn.setVisibility( View.VISIBLE );
                    mProfileDeclineReqBtn.setEnabled( true );
                } else {
                    mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                    mProfileDeclineReqBtn.setEnabled( false );
                }


                /*Picasso.with( ProfileActivity.this )
                        .load( display_image )
                        .placeholder( R.drawable.no_profile )
                        .into( mProfileImage );*/

                Picasso.with( ProfileActivity.this )
                        .load( display_image )
                        .networkPolicy( NetworkPolicy.OFFLINE )
                        .placeholder( R.drawable.no_profile )
                        .into( mProfileImage, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError() {
                                Picasso.with( ProfileActivity.this )
                                        .load( display_image )
                                        .placeholder( R.drawable.no_profile )
                                        .into( mProfileImage );
                            }
                        } );


                // ========== FRIEND LIST ONLY FOR CURRENT USER LOGGED IN ==========
                mFriendsReqDatabase.child( mCuurentUser.getUid() ).addListenerForSingleValueEvent( new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChild( user_id )) {
                            String req_type = dataSnapshot.child( user_id ).child( "request_type" ).getValue().toString();
                            if (req_type.equals( "received" )) {
                                mCuurent_state = "req_received";
                                mProfileSendReqBtn.setText( "Accept Friend Request" );

                                mProfileDeclineReqBtn.setVisibility( View.VISIBLE );
                                mProfileDeclineReqBtn.setEnabled( true );

                            } else if (req_type.equals( "sent" )) {
                                mCuurent_state = "req_sent";
                                mProfileSendReqBtn.setText( "Cancel Friend Request" );
                                mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );

                                mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                                mProfileDeclineReqBtn.setEnabled( false );

                            }

                        } else {

                            /*Toast.makeText(getApplicationContext(), "TIDAK ADA : " + mCuurent_state, Toast.LENGTH_SHORT).show();*/

                            mFriendsDatabase.child( mCuurentUser.getUid() ).addListenerForSingleValueEvent( new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if (dataSnapshot.hasChild( user_id )) {
                                        mCuurent_state = "friends";
                                        mProfileSendReqBtn.setText( "Unfriend This Person" );
                                        /*mProfileSendReqBtn.setEnabled( false );
                                        mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );*/

                                        mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                                        mProfileDeclineReqBtn.setEnabled( false );
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            } );

                        }
                        mDialog.dismiss();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                } );
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        } );

        mProfileSendReqBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // ========== ADD FRIEND REQUEST ==========
                if (mCuurent_state.equals( "not_friends" )) {

                    showLoading( "Data is processing...!" );
                    mProfileSendReqBtn.setEnabled( false );

                    mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).child( "request_type" ).setValue( "sent" ).addOnCompleteListener( new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).child( "request_type" ).setValue( "received" ).addOnCompleteListener( new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {

                                            HashMap<String, String> notificationData = new HashMap<>(  );
                                            notificationData.put( "from", mCuurentUser.getUid() );
                                            notificationData.put( "type", "request" );

                                            mNotificationDatabase.child( user_id ).push().setValue( notificationData ).addOnSuccessListener( new OnSuccessListener<Void>() {
                                                @Override
                                                public void onSuccess(Void aVoid) {
                                                    mDialog.dismiss();
                                                    mCuurent_state = "req_sent";
                                                    mProfileSendReqBtn.setText( "Cancel Friend Request" );
                                                    mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );

                                                    mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                                                    mProfileDeclineReqBtn.setEnabled( false );

                                                    showSuccess( "Request Sent!" );
                                                }
                                            } );


                                        } else {
                                            mDialog.dismiss();
                                            onError( "Request Submission Failed!" );
                                        }
                                    }
                                } );
                            } else {
                                mDialog.dismiss();
                                onError( "Request Submission Failed!" );
                            }
                            mProfileSendReqBtn.setEnabled( true );
                        }
                    } );
                }


                // ========== CANCEL REQUEST FRIEND ==========
                if (mCuurent_state.equals( "req_sent" )) {

                    showLoading( "Data is processing...!" );
                    mProfileSendReqBtn.setEnabled( false );

                    mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            mDialog.dismiss();
                                            mCuurent_state = "not_friends";
                                            mProfileSendReqBtn.setText( "Send Friend Request" );
                                            mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.successAlert ) );

                                            mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                                            mProfileDeclineReqBtn.setEnabled( false );

                                            onSuccess( "Cancel Request Success!" );
                                        } else {
                                            mDialog.dismiss();
                                            onError( "Cancel Request Failed!" );
                                        }
                                    }
                                } );
                            } else {
                                mDialog.dismiss();
                                onError( "Cancel Request Failed!" );
                            }
                            mProfileSendReqBtn.setEnabled( true );
                        }
                    } );
                }


                // ========== ACTION APPROVE REQUEST ==========
                if (mCuurent_state.equals( "req_received" )) {


                    showLoading( "Data is processing...!" );
                    mProfileSendReqBtn.setEnabled( false );

                    final String currentDate = DateFormat.getDateTimeInstance().format( new Date() );

                    mFriendsDatabase.child( mCuurentUser.getUid() ).child( user_id ).setValue( currentDate ).addOnCompleteListener( new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                mFriendsDatabase.child( user_id ).child( mCuurentUser.getUid() ).setValue( currentDate ).addOnSuccessListener( new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {

                                        mFriendsReqDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {
                                                    mFriendsReqDatabase.child( user_id ).child( mCuurentUser.getUid() ).removeValue().addOnCompleteListener( new OnCompleteListener<Void>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<Void> task) {
                                                            if (task.isSuccessful()) {
                                                                mDialog.dismiss();
                                                                mCuurent_state = "friends";
                                                                mProfileSendReqBtn.setText( "Unfriend This Person" );
                                                                mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.colorAccent ) );

                                                                mProfileDeclineReqBtn.setVisibility( View.INVISIBLE );
                                                                mProfileDeclineReqBtn.setEnabled( false );

                                                                showSuccess( "Success approve friend request!" );
                                                            } else {
                                                                mDialog.dismiss();
                                                                onError( "Failed approve friend request!" );
                                                            }
                                                        }
                                                    } );
                                                } else {
                                                    mDialog.dismiss();
                                                    onError( "Failed approve friend request!" );
                                                }
                                                mProfileSendReqBtn.setEnabled( true );
                                            }
                                        } );

                                    }
                                } );
                            } else {
                                mDialog.dismiss();
                                onError( "Failed approve friend request!" );
                            }
                            mProfileSendReqBtn.setEnabled( true );
                        }
                    } );
                }

                // ========== ACTION REMOVE FRIENDS ==========
                if (mCuurent_state.equals( "friends" )) {
                    showLoading( "Data is processing...!" );
                    mFriendsDatabase.child( mCuurentUser.getUid() ).child( user_id ).removeValue().addOnSuccessListener( new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            mDialog.dismiss();
                            mCuurent_state = "not_friends";
                            mProfileSendReqBtn.setText( "Send Friend Request" );
                            mProfileSendReqBtn.setBackgroundColor( getResources().getColor( R.color.successAlert ) );
                        }
                    } );
                }

            }
        } );

    }

    private void showLoading(String s) {
        mDialog.setTitle( "Please wait a moment" );
        mDialog.setMessage( s );
        mDialog.setCanceledOnTouchOutside( false );
        mDialog.show();
    }

    public void showSuccess(String message) {
        Alerter.create( this )
                .setTitle( "Success" )
                .setText( message )
                .setBackgroundColorRes( R.color.successAlert )
                .show();
    }
}

MyFirebaseMessagingService.java

package com.bertho.chat;


import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCM Service";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived( remoteMessage );

        String notificationTitle = remoteMessage.getNotification().getTitle();
        String notificationBody = remoteMessage.getNotification().getBody();

        String click_action = remoteMessage.getNotification().getClickAction();

        String from_user_id = remoteMessage.getData().get( "from_user_id" );

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder( this )
                        .setSmallIcon( R.drawable.notif_icon )
                        .setContentTitle( notificationTitle )
                        .setContentText( notificationBody );

        Intent resultIntent = new Intent(click_action);
        resultIntent.putExtra( "user_id",  from_user_id);

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);


        int mNotificationId = (int) System.currentTimeMillis();

        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
        mNotifyMgr.notify( mNotificationId, mBuilder.build() );

    }
}

根据描述错误日志是指这一行

mUserDatabase = FirebaseDatabase.getInstance().getReference().child( "Users" ).child( user_id );

在这种情况下,应用程序是否无法从 Firebase 数据库加载数据以致崩溃?

请指教

最佳答案

我更改了我的 Firebase 函数

const payload = {
    notification: {
        title: "New Friend Request",
        body: `${userName} has sent you Friend Request`,
        icon: "default",
        click_action: "com.bertho.chat_TARGET_NOTIFICATION"
    },
    data: {
        from_user_id: from_user_id
    }
};

const payload = {
    data: {
        title: "New Friend Request",
        body: `${userName} has sent you Friend Request`,
        from_user_id: from_user_id,
        click_action: "com.bertho.chat_TARGET_NOTIFICATION"
    }
};

感谢@UmarZaii

关于java - 火力地堡 : App crash when notification comes with app not opened,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388110/

相关文章:

java - 将内容适配到 JavaFx 中的 WebView

java - Maven Profile 从 Properties 文件读取数据

Android:在方向更改时更改 CardView 布局

Android 2d Canvas 游戏: FPS Jitter problem

Android ExpandedListView动画展开慢而流畅

java - 如何在 Android 上获取表列数组列表?

java - JNI/外部库调用是否需要直接缓冲区?

java - db2,如果时间戳早于 7 年,则获取前 20 条记录

java - C# out IntPtr 在 Java 中等效

java - 如何修复过时的 "getDownloadUrl ()"代码?