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

问题描述 投票:0回答:1

我想知道如何保存从firebase云消息传递的数据有效负载收到的信息。实际上我想要的是,我从数据有效负载接收到名为notification_type和其他内容的字段,我希望在应用程序在后台时保存它,并在我打开应用程序并基于此更新我的UI时检索它。

直到现在我已经尝试将它保存在Sharedprefrences,外部文件上,最后保存在本地数据库上,所有这些工作正常并在应用程序处于前台时保存数据,但是当应用程序在后台时它们不起作用。

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

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();
    }
}
android firebase sqlite firebase-cloud-messaging
1个回答
0
投票

我在firebase文档的基础知识上出错了,因为在文档中明确表示如果通知有效负载在那里,那么通知将由firebase sdk而不是onMessageReceived方法处理,所以我改变了发送数据有效负载和通知有效负载只是数据有效负载,之后,当我收到数据有效负载通知时,调用onMessageReceived,我可以将数据有效负载插入我的本地数据库。所以我把我的代码改成了这样的东西

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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.