当应用程序在后台运行时,如何保存收到的通知中的数据

时间:2019-03-26 10:43:46

标签: android firebase sqlite firebase-cloud-messaging

我想知道如何保存从Firebase云消息传递的数据有效载荷接收的信息。 我真正想要的是,我从数据有效负载中接收到名为notification_type和其他内容的字段,并且想要在应用程序处于后台时将其保存在某处,并在我打开应用程序并基于此更新UI时将其检索。

到目前为止,我尝试将其保存在Sharedprefrences,外部文件以及最后在本地db上,所有这些都工作正常,并且在应用程序位于前台时保存了数据,但是在应用程序位于后台时它们却无法工作

下面是我最近尝试的用于将数据保存在本地数据库中的代码,当应用程序在前台时可以正常工作,但是在应用程序在后台时无法正常工作。

FireBaseMsgService.class

public class FireBaseMsgService extends FirebaseMessagingService {
     @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       // for normal notifications
        super.onMessageReceived(remoteMessage);
        if(remoteMessage.getData().get("Notification_type").equals("2")){
            String Org_Id=remoteMessage.getData().get("Org_Id");
            String Unit_Id=remoteMessage.getData().get("Unit_Id");
            String User_Id=remoteMessage.getData().get("User_Id");
            String PatientId=remoteMessage.getData().get("PatientId");
            String Notification_type=remoteMessage.getData().get("Notification_type");
            if (remoteMessage.getNotification() != null){
                String Click_Action=remoteMessage.getNotification().getClickAction();
                User_Id=remoteMessage.getNotification().getTag();
                saveNotificationDataToDb(getApplicationContext(),Notification_type);
                FirebaseNotificationService firebaseNotificationService=new FirebaseNotificationService(
                        getApplicationContext(),
                        remoteMessage.getNotification().getTitle(),
                        remoteMessage.getNotification().getBody(),
                        Click_Action,
                        User_Id,
                        Org_Id,
                        Unit_Id,
                        PatientId,
                        Notification_type);
                firebaseNotificationService.createNotification(remoteMessage.getNotification().getTitle(),
                        remoteMessage.getNotification().getBody(),
                        User_Id);
                BusProvider.postOnMain(BusProvider.getInstance(),new NotifyingEvent(Notification_type));

                //sendNotificationToFilesystem(Notification_type);

            }
        }
        //for normal notifications
        else{
            if (remoteMessage.getNotification() != null){
                String User_Id=remoteMessage.getNotification().getTag();
                FirebaseNotificationService firebaseNotificationService=new FirebaseNotificationService(
                        getApplicationContext(),
                        remoteMessage.getNotification().getTitle(),
                        remoteMessage.getNotification().getBody(),
                        User_Id);
                firebaseNotificationService.createNotification(remoteMessage.getNotification().getTitle(),
                        remoteMessage.getNotification().getBody(),
                        User_Id);
            }
        }
    }

    private void saveNotificationDataToDb(Context context , String notification_type) {
        RecievedNotificationDbtable recievedNotificationDbtable=new RecievedNotificationDbtable(context);
        recievedNotificationDbtable.open();
        recievedNotificationDbtable.insertEntry(notification_type);
        recievedNotificationDbtable.close();
    }
}

1 个答案:

答案 0 :(得分:0)

在firebase文档的基础上,我错了,因为在文档中明确指出,如果存在通知有效负载,那么通知将由firebase sdk处理,而不是onMessageReceived方法,因此我改变了发送两者数据有效载荷和通知有效载荷,只是数据有效载荷,此后,每当我收到数据有效载荷的通知时,就会调用onMessageReceived,并且可以将数据有效载荷插入到本地db中。所以我将代码更改为类似

FirebaseMsgService.class

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    if(remoteMessage.getData().get("Notification_type").equals("2")){
            String Org_Id=remoteMessage.getData().get("Org_Id");
            String Unit_Id=remoteMessage.getData().get("Unit_Id");
            String User_Id=remoteMessage.getData().get("User_Id");
            String PatientId=remoteMessage.getData().get("PatientId");
            String Notification_type=remoteMessage.getData().get("Notification_type");
            String Click_Action=remoteMessage.getData().get("click_action");
            String title=remoteMessage.getData().get("title");
            String body=remoteMessage.getData().get("body");
            FirebaseNotificationService firebaseNotificationService=new FirebaseNotificationService(
                        getApplicationContext(),
                        title,
                        body,
                    Click_Action,
                        User_Id,
                        Org_Id,
                        Unit_Id,
                        PatientId,
                        Notification_type);
                firebaseNotificationService.createNotification(title,
                        body,
                        User_Id);
            saveNotificationDataToDb(getApplicationContext(),Notification_type);
            BusProvider.postOnMain(BusProvider.getInstance(),new NotifyingEvent(Notification_type));

        }
}

并使用此方法保存到db

private void saveNotificationDataToDb(Context context , String notification_type) {
        RecievedNotificationDbtable recievedNotificationDbtable=new RecievedNotificationDbtable(context);
        recievedNotificationDbtable.open();
        recievedNotificationDbtable.insertEntry(notification_type);
        recievedNotificationDbtable.close();
    }