在Firebase中获取一对一聊天应用通知错误

时间:2017-07-05 11:00:02

标签: android firebase firebase-realtime-database firebase-cloud-messaging

我已经制作了一对一的聊天应用程序并且我已经实现了聊天通知,但是当我从用户应用程序向医生应用程序发送通知时,我的医生应用程序在我点击聊天通知时立即崩溃

这是我的Java类,我在处理通知,如果我的应用程序在前台,所以这个类将处理通知,所以现在在这个类我调用firebase数据库并从那里获取一些数据并将该数据作为额外数据传递与结果意图,但当结果意图将应用程序指向特定的聊天视图时,数据库数据不存在

这是我的应用程序的日志

E/MyFirebaseMessaging: From:408315985482
07-05 16:19:33.803 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: in between Firebase and auth
07-05 16:19:33.805 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: Message data payload: {idRe, username=komal , message=hi}
07-05 16:19:33.805 4399-6278/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: app in background
07-05 16:19:33.813 4399-6278/com.tech.pritz.bemoprovider E/MyFirebaseMessaging: image not found
07-05 16:19:33.825 4399-6278/com.tech.pritz.bemoprovider D/NotificationUtil: New Message

D/MyFirebaseMessaging: Entered into the database

07-05 16:19:34.359 4399-4399/com.tech.pritz.bemoprovider D/MyFirebaseMessaging: HKNi1sezUpbwJSeEGos44FtfUw43

你可以在这里看到它也显示在数据库中输入并且它也获得了那些数据但是当我作为意图传递时它在另一个活动中找不到。

崩溃日志

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法' java.lang.String   com.tech.pritz.bemoprovider.model.Provider.getId()'在null对象上   参考   com.tech.pritz.bemoprovider.bemo.ChatView.ini(ChatView.java:350)at at   com.tech.pritz.bemoprovider.bemo.ChatView.onCreate(ChatView.的java:150)

  public class MyFirebaseMessaging extends FirebaseMessagingService
    {

        private  static  final String TAG=MyFirebaseMessaging.class.getSimpleName();
        private NotificationUtil notificationUtil;
        private DatabaseReference pathFirebase;
        private FirebaseAuth auth;
        private Provider mprovider;

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage)
        {
            Log.e(TAG,"From:"+ remoteMessage.getFrom());
            pathFirebase = FirebaseDatabase.getInstance().getReference();
            Log.d(TAG,"in between Firebase and auth");
            auth = FirebaseAuth.getInstance();



            if(remoteMessage ==null )
            {
                return;

            }
            //check if message contains a notification payload
            if(remoteMessage.getNotification()!=null)
            {
                String tittle = remoteMessage.getNotification().getTitle();
                Log.e(TAG,"Notification Body:" + remoteMessage.getNotification().getBody());
                handleNotification(remoteMessage.getNotification().getBody(),tittle,null, mprovider);
            }
            //checks to see if message contains a data payload
            if(remoteMessage.getData().size() > 0)
            {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
               String imageUrl = remoteMessage.getData().get("image");
              //  String message =  remoteMessage.getData().get("message");
               // String title = remoteMessage.getData().get("title");
               // String name = remoteMessage.getData().get("username");

               String message= remoteMessage.getData().get("message");
                String name= remoteMessage.getData().get("username");
                String title= "New Message";
                String iduser= remoteMessage.getData().get("iduser");
                String email=  remoteMessage.getData().get("email");
                String gcm_id=  remoteMessage.getData().get("idGCM");
                String conversation_id=remoteMessage.getData().get("conversation_id");

                Log.d(TAG,"app in background");



                pathFirebase.child("users").child(auth.getCurrentUser().getUid())
                        .addValueEventListener(
                                new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {

                                        if (dataSnapshot.exists()) {
                                            Log.d(TAG,"Entered into the database");
                                            mprovider = dataSnapshot.getValue(Provider.class);
                                            UserDataHolder.getInstance().setmName(mprovider.getName());
                                            // Intent intent = new Intent(MyFirebaseMessaging.this, ListPatients.class);
                                            mprovider.setId(auth.getCurrentUser().getUid());
                                            UserDataHolder.getInstance().setmUserId(mprovider.getId());
                                            // intent.putExtra("user", provider);
                                            //startActivity(intent);
                                            //finish();
                                            Log.d(TAG,mprovider.getId());
                                        }
                                    }
                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                        Toast.makeText(MyFirebaseMessaging.this, "Failed", Toast.LENGTH_SHORT).show();
                                    }
                                });


    //            Log.d(TAG,mprovider.getId());
                // app is in background, show the notification in notification tray
                Intent resultIntent = new Intent(getApplicationContext(),ChatView.class);
                resultIntent.putExtra("user_id", iduser);
                resultIntent.putExtra("email", email);
                resultIntent.putExtra("User_display_name",name);
                resultIntent.putExtra("user_gcm_id",gcm_id);
                resultIntent.putExtra("conversation_id",conversation_id);
               // resultIntent.putExtra("providerLocal",mprovider);


                resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //  resultIntent.putExtra("message", message);

                // check for image attachment
                if (TextUtils.isEmpty(imageUrl))
                {
                    Log.e(TAG,"image not found");
                    showNotificationMessage(getApplicationContext(), title, message,name,resultIntent,mprovider);
                }
                else
                {
                    // image is present, show notification with image
                    Log.e(TAG,"image found");
                    showNotificationMessageWithBigImage(getApplicationContext(), title, message, resultIntent, imageUrl);
                }
            }

        }


        private void handleNotification(String message,String title,String name,Provider provider)
        {

            if(!NotificationUtil.isAppInBackground(getApplicationContext()))
            {
                Intent resultIntent = new Intent(getApplicationContext(), ChatView.class);
                showNotificationMessage(getApplicationContext(), title, message,null,resultIntent, provider);

            }
            else
            {
                Intent resultIntent = new Intent(getApplicationContext(), ChatView.class);
                showNotificationMessage(getApplicationContext(), title, message,null,resultIntent, mprovider);

            }
        }

        private void showNotificationMessageWithBigImage(Context applicationContext, String title, String message, Intent resultIntent, String imageUrl)
        {
            notificationUtil = new NotificationUtil(applicationContext);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            notificationUtil.showNotificationMessage(title, message,null,resultIntent,imageUrl);

        }

        private void showNotificationMessage(Context applicationContext, String title, String message,String name, Intent resultIntent,Provider mprovider)
        {
           // Log.d(TAG,mprovider.getId());
            notificationUtil = new NotificationUtil(applicationContext);
            resultIntent.putExtra("providerLocal",mprovider);
            // resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            notificationUtil.showNotificationMessage(title, message,name,resultIntent);


        }

    }

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

UserDataHolder.getInstance().setmUserId(mprovider.getId());

mprovider对象为空