【问题标题】:How to store firebase push notification in sqlite when app in background, foreground and closed state in Xamarin Android当应用程序在 Xamarin Android 中处于后台、前台和关闭状态时,如何在 sqlite 中存储 Firebase 推送通知
【发布时间】:2019-07-23 09:49:00
【问题描述】:

当应用程序处于关闭/后台/前台等任何状态时,我想将 firebase 推送通知存储到 sqlite。我怎样才能做到这一点?可以这样做吗?

【问题讨论】:

  • 你只在android中使用这个吗?
  • 是的,xamarin android
  • 好的,这很容易,尽快给我添加代码
  • 是的,请......

标签: firebase sqlite xamarin push-notification firebase-cloud-messaging


【解决方案1】:

这里是这个问题的最终解决方案,感谢 Mr.G.hakim

public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(TAG, "From: " + message.From);
        Log.Debug(TAG, "Notification Message Body: " + getBody);
        SendNotification(getBody, message.Data);
    }

    public override void HandleIntent(Intent p0)// this method will fire when the app in background and closed state
    {
        base.HandleIntent(p0);
        if (p0.Extras != null)
        {
            foreach (var key in p0.Extras.KeySet())
            {
                var value = p0.Extras.GetString(key);
                Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                if(key== "gcm.notification.title")
                {
                    Log.Debug("Delay Notification Title", "" + value);
                    getBGTitle = value;//declared local variable
                }
                else if(key== "gcm.notification.body")
                {
                    Log.Debug("Delay Notification Body", "" + value);
                    getBGBody = value;//declared local variable
        insertData(getBGTitle,getBGBody)//call method for store SQLite Insert
                }
            }
        }
}

【讨论】:

    【解决方案2】:

    在您的FirebaseMessagingService 继承类中,您有两个重要的方法可以用于此目的:

    对于前台通知:

    当您的应用程序处于前台状态时调用此方法

    public override void OnMessageReceived(RemoteMessage message)
        {
           // SQLite saving code here 
        }
    

    对于 Background 和 Killed 状态通知:

    当您的应用程序处于后台/终止状态时调用此方法

     public override void HandleIntent(Intent p0)
        {
           base.HandleIntent(p0);
           // SQLite saving code here 
        }
    

    注意:在某些情况下,此函数也会在可能导致重复的前台通知中调用。

    【讨论】:

    • 很好。但是在这里我怎样才能获得像“var getBody = message.GetNotification().Body;”这样的通知内容
    • 如果您检查此方法中的参数意图,它具有 Extras 属性,即 p0.Extras 这是一个 Android.OS.Bundle,您可以使用 GetString 方法获取其中的数据只需传递在调试此属性时可以找到的相关键中
    • 嗯,基本上你必须调试这个属性并在其中找到你的消息
    • 检查this
    • 好吧,在这种情况下,你必须创建自定义通知:stackoverflow.com/a/40728153/7462031 一旦你这样做了,你的所有通知都会调用 OnMessageReceived 方法
    【解决方案3】:

    当您通过OnMessageReceived 方法收到通知时,请使用此方法检查应用状态

    private bool isApplicationInTheBackground()
    {
        bool isInBackground;
    
        RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
        ActivityManager.GetMyMemoryState(myProcess);
        isInBackground = myProcess.Importance != Android.App.Importance.background;
    
        return isInBackground;
    }
    

    如果您的应用处于background 状态,请从OnMessageReceived 获取通知信息并将其插入所需的表中

    public override void OnMessageReceived(RemoteMessage message)
    {
        string orgId;
        var data = message.Data.ToDictionary(i => i.Key, i => i.Value);
        if (data.ContainsKey("organization"))
            orgId = data["organization"];
    
    
        if(isApplicationInTheBackground())
        {
            var db = GetCon();
            db.Insert(orgId); //here should be your table.
        }
    }
    

    当您想要发送/传递保存的通知时,请使用 本地 android 推送通知

    【讨论】:

    • 这适用于应用关闭状态吗?
    • 应用处于后台&关闭状态时如何调用OnMessageReceived()?
    • 无论应用程序状态如何,只要发送此方法的通知就会自动被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多